Solved

mod_rewrite rewriterule relative links

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 69
Use of SSL Certificates - http vs https 3 90
PHP in Apache server 20 104
Apache,  not running php on top level 14 73
If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

763 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