Solved

mod_rewrite rewriterule relative links

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hosting application in Apache and Tomcat 1 104
How to analyze web traffic logs 10 108
Increase number of concurrent connections in Apache tomcat 6 2 80
maybe no no httpd.conf 6 47
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…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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