Solved

mod_rewrite rewriterule relative links

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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