mod_rewrite : why does it work locally and not online?
-
03-07-2019 - |
Question
I'm developing an app locally (under Domain name <mydomain>.dev).
In order to work with friendly urls, i've set up my .htaccess like this:
RewriteEngine on
# Externally redirect to add missing trailing slash
RewriteRule ^(([a-z0-9._\-]+/)*[a-z0-9_\-]+)$ http://example.com/$1/?%{QUERY_STRING}[NC,R,L]
RewriteRule ^about/$ about.php [NC,L]
RewriteRule ^issues/$ issues.php [NC,L]
RewriteRule ^issue/([a-z0-9_\-]+)/$ issue.php?slug=$1 [NC,L]
SetEnv PHP_VER 5
IndexIgnore *
Options +FollowSymLinks
It works fine. Annoyingly, when going online, it's not so great:
http://example.com/issue/my-slug/#23 returns no GET variable. Why?
Solution
My guess, based on past experience is RewriteBase, likely because your on a shared server, or some other non-standard configuration.
RewriteEngine on
#Set base as doc root.
RewriteBase /
# Externally redirect to add missing trailing slash
RewriteRule ^(([a-z0-9._\-]+/)*[a-z0-9_\-]+)$ http://mydomain.eu/$1/?%{QUERY_STRING}[NC,R,L]
RewriteRule ^about/$ about.php [NC,L]
RewriteRule ^issues/$ issues.php [NC,L]
RewriteRule ^issue/([a-z0-9_\-]+)/$ issue.php?slug=$1 [NC,L]
SetEnv PHP_VER 5
IndexIgnore *
Options +FollowSymLinks
OTHER TIPS
I don't see that
RewriteRule ^(([a-z0-9._\-]+/)*[a-z0-9_\-]+)$ http://mydomain.eu/$1/?%{QUERY_STRING}[NC,R,L]
should match that URL, but thats the only one with the [R] flag to do an external redirect. Try commenting that line out, to make sure there is not some other part of your application doing the redirect. My guess is that there is.
There is some whitespace missing between the substitution URL and the flags. You can also simplify your first rule as follows:
RewriteRule ^([a-z0-9._-]+/)*[a-z0-9_-]+$ %{REQUEST_URI}/ [NC,R=301,L]