-
-
Save yegnold/5901983 to your computer and use it in GitHub Desktop.
RewriteEngine On | |
# add-ons rewrite rule. Note absence of slash in "rewrite to" (second portion of rewriterule) | |
# this makes the rewrite relative to the directroy of the .htaccess file unless RewriteBase is defined | |
RewriteRule ^add-ons/apps/([a-zA-Z0-9-/]+)$ add-ons/apps.php?s=$1 [L] | |
# The "most generic" rule comes last, as "more specific" rules should match first. | |
# Added a check to see if we've already got .php in the URL. | |
RewriteCond %{REQUEST_URI} !.php$ | |
RewriteRule ^(.+)$ $1.php [L,QSA] | |
------------------ | |
COLES HTACCESS ORIGINAL | |
----------------- | |
RewriteEngine on | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA] | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteCond %{REQUEST_FILENAME}.php -f | |
RewriteRule ^add-ons/apps/([a-zA-Z0-9-/]+)$ add-ons/apps.php?s=$1 [L] | |
RewriteRule ^(.+)$ $1.php [L,QSA] | |
---------------- | |
EDS UPDATED - Use line 31 onwards in your htaccess file for a working version | |
---------------- | |
RewriteEngine on | |
# The specific add-ons rewrite rule should come first as it is most specific. | |
RewriteRule ^add-ons/apps/([a-zA-Z0-9-/]+)$ add-ons/apps.php?s=$1 [L] | |
# Next, the rule to remove trailing slashes. | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA] | |
# Finally, the "catch all" redirect. | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteCond %{REQUEST_FILENAME}.php -f | |
RewriteRule ^(.+)$ $1.php [L,QSA] |
Browsers will only "cache" redirects, a redirect being an instruction to change the location of the browser rather than "map" the request to another file or local resource.
So
RewriteRule ^pattern$ http://www.domain.com/example.org [L]
is a "redirect"
RewriteRule ^pattern$ local_resource.php [L]
is a "rewrite"
Thanks Ed - getting following Apache error message when try this:
[Mon Jul 01 16:48:06 2013] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Seems to be caught in a loop :/
I get the same, which is encouraging as it means we are definitely doing something wrong :)
OK
The rule:
RewriteRule ^add-ons/apps/([a-zA-Z0-9-/]+)$ add-ons/apps.php?s=$1 [L]
is fine.
It is a problem with
RewriteRule ^(.+)$ $1.php [L,QSA]
I believe that this rule "maps back" to itself, i.e. asda redirects to asda.php with redirects to asda.php.php which redirects to asda.php.php.php.
Hmmm, there is a RewriteCond %{REQUEST_FILENAME} !-f that should be preventing redirects on files.
Is there a way to put a RewriteCond to eg ignore anything already with a PHP suffix?
Here's the htaccess in full:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^add-ons/apps/([a-zA-Z0-9-/]+)$ add-ons/apps.php?s=$1 [L]
RewriteRule ^(.+)$ $1.php [L,QSA]
This is my working test case, I think:
RewriteEngine On
RewriteRule ^add-ons/apps/([a-zA-Z0-9-/]+)$ add-ons/apps.php?s=$1 [L]
RewriteCond %{REQUEST_URI} !.php$
RewriteRule ^(.+)$ $1.php [L,QSA]
Was returning error where directories were redirecting too.
However reiterating earlier RewriteCond seems to have fixed!
RewriteEngine on
# rules go here
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond $1 !\.php$
RewriteRule ^add-ons/apps/([a-zA-Z0-9-/]+)$ add-ons/apps.php?s=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
Works wouldn't expect to have to (re)state the conditions twice!
Thanks for all your help with this Ed!
Hi cole
a "working implementation" is included in the gist at the top of this page if you refresh
The reason it wasn't working is because the RewriteCond rules (which say that the subsequent rewrite rule should apply only if those conditions are met) only apply to the next rewrite rule. I.e. they apply only to the rewrite rule immediately afterwards, not any other subsequent rewrite rules.
Because you had placed a new rewrite rule in the middle of the conditions and the "catch all" redirect, the conditions were no longer applying to the "catch all" redirect and so the check (does this filename exist in php form - the line that says %{REQUEST_FILENAME}.php -f) - was not being executed for the catch all redirect.
Hopefully his makes sense
If you wanted it to be possible to visit add-ons/apps/9?param=value then the first rule would need to be modified to be:
RewriteRule ^add-ons/apps/([a-zA-Z0-9-/]+)$ add-ons/apps.php?s=$1 [L,QSA]