Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

One of our developers who built the framework that our website uses is no longer with us and I appear to have a stack of issues across the site currently, one being URL rewriting across the site.

This is what we have at the moment:-

RewriteRule ^(.*)/(.*)$  index.php?page_name=$1&sub=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$

This currently rewrites URL's correctly in the sense that...

domain.com/index.php?page_name=products would be rewritten to domain.com/products

But if you try to access a path that exists on the server but not in the database such as.

domain.com/fonts this is trying to be rewritten to domain.com/fonts/?page_name=fonts&sub= and throws a redirect loop error in the browser.

I think I need to be rewriting directory paths as well as what we are currently - I'm not too sure whether this is code that has to be rewritten or whether we can easily achieve this from an addition rule in .htaccess.

Edit: To include full rewrite sections from domain following on from Justin's answer below...

RewriteEngine on

RewriteCond %{REQUEST_URI} !\.(jpg|png|gif|css|js|get.php|addon.php|script.php|rss.php|favicon.ico|feed.php|301.php|oldsite/|clientarea/|htc|PIE.php)$
RewriteRule ^(.*)/(.*)$  index.php?page_name=$1&sub=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$

## FIXES TRAILING SLASH ##
RewriteRule (.*)([^/])$ http://www.domain.com/$1$2/ [R=301,L]

RewriteRule ^$ home/ [NC,L]

RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} ^/robots.txt$
RewriteRule ^(.*)$ /robots_https.txt [L]
share|improve this question
add comment

1 Answer 1

Now that I can see there were lines before what you had, I think you need to change the first lines of the file as such:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$  index.php?page_name=$1&sub=$2 [NC,L]

The problem stems from the fact that RewriteRule ^(.*)/(.*)$ is running whether or not it's a file or directory, with the exception of everything in the preceeding RewriteCond %{REQUEST_URI} line. In my changes, I removed the RewriteCond %{REQUEST_URI} line because it accomplishes the same as the RewriteCond %{REQUEST_FILENAME} !-f, which should take care of any image, js, css, and other files itself. It won't hurt anything to leave it in there, but it is redundant.

I think the issue is that the RewriteCond need to go above the RewriteRule like so:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)/(.*)$  index.php?page_name=$1&sub=$2 [NC,L]

The RewriteCond are the conditions that must first be met before the RewriteRule is engaged.

  • RewriteCond %{REQUEST_FILENAME} !-f checks to see if the 'file' part of the url is actually a real file on the server.
  • RewriteCond %{REQUEST_FILENAME} !-d is the same as the above, but specifically for directories.
  • RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ checks to see if the request part of the URL ends in a '.' and an alphanumeric string.

If all of these conditions are met, then it does the rewrite. As you have it now, it does the rewriting and completely skips the conditions for the rewrite, hence why URLs to existing folders/files are getting rewritten.

share|improve this answer
    
Thanks for your reply Justin, I understand what you're saying - unfortunately in this case, it broke the site and nothing loaded at all. I've edited my original question to include all rewrite conditions/rules in case this helps identify if they are in the wrong order. (When I tried your suggestion, every URL on the site just returned 'Not Found' on the server). Thanks for your time. –  zigojacko May 10 '12 at 9:41
    
@GeoffJackson Please see my revised answer based on your new information. –  Justin Pearce May 10 '12 at 13:48
    
Hi Justin, thanks for your revised answer. After implementing your new suggestion, pages on the site still loaded but the URL's in the address bar were appearing like this domain.com/index.php/?page_name=about-us&sub= –  zigojacko May 11 '12 at 10:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.