Solved

mod_rewrite rewriterule relative links

Posted on 2004-10-06
3
649 Views
Last Modified: 2012-08-13
Hi EE,

I'm using mod_rewrite and rewrite rule for user-friendly URLs, and it's working fine.  The only time it fails me is if the user accidentally includes a trailing slash in the URL.

Here's my /.htaccess:

Options FollowSymLinks
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([-.a-zA-Z_0-9]+)/?$ /page.php?pagealias=$1 [NC]

This says, if URL contains a file or directory that exists, go there.  If not, compile into a variable and send to page.php (which uses the querystring $1 to pull dynamic content).

If the user enters, http://www.site.com/fire-protection
the rewriterule works, and the relative links for things like images and css files work fine.

If the user enters, http://www.site.com/fire-protection/
the rewriterule works, but the relative links no longer work.  

Can i eliminate the trailing slash and then do the rewriterule?  Or, is there some way to easily create relative links as absolute links, without hard coding each link?
0
Comment
Question by:bradnoble
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
TomDavidson earned 125 total points
ID: 12238965
The browser is interpreting the relative link differently.
For exaple a link to ./dir/anotherpage.html would be:

http://www.site.com/fire-protection:
http://www.site.com/dir/anotherpage.html

http://www.site.com/fire-protection/:
http://www.site.com/fire-protection/dir/anotherpage.html

probably the easyest was round is to use absolute links, especially if resource like images are in a directory.

The other option - you could rewrite all requests to ignore the directory:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /([\-\.a-zA-Z_0-9]+)/?$ /$1 [NC]
may do it

0
 

Author Comment

by:bradnoble
ID: 12300319
Thanks for your help.  I simply made my links relative and that worked fine!
0
 

Author Comment

by:bradnoble
ID: 12300332
WHOOPS!  I mean, i simply made the links absolute!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to get function 52 123
Best IDE for PHP starters 11 151
HHTP and HTTPS redirect question 3 145
Redirect old website to new website in subfolder using .htaccess 2 54
Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question