Mobile app version of vmapp.org
Login or Join
Harper822

: Mod_rewrite for directories 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

@Harper822

Posted in: #Htaccess #ModRewrite

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=&sub= [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=&sub= [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$

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

RewriteRule ^$ home/ [NC,L]

RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} ^/robots.txt$
RewriteRule ^(.*)$ /robots_https.txt [L]

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Harper822

1 Comments

Sorted by latest first Latest Oldest Best

 

@Yeniel560

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=&sub= [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=&sub= [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme