Solved

mod_rewrite rewriterule relative links

Posted on 2004-10-06
3
641 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
  • 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now