I\'m trying to figure out how to do an apache mod_rewrite
to remap $_GET
.
What I\'m trying to accomplish:
Currentl
Basically, you need two rules. One rule to redirect the client to a clean URL and another to internally rewrite the pretty URL to the real content via index.php
.
Assuming the index.php
and .htaccess
is in a directory cdi
RewriteEngine on
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# redirect the client
RewriteCond %{QUERY_STRING} URL=pages/(.+?)\.php
RewriteRule ^index\.php$ /cdi/%1? [R,L]
# exclude rewriting all files located in /cdi/files
RewriteCond %{REQUEST_URI} !^/cdi/files/
# rewrite to real content
RewriteRule ^.*$ /cdi/index.php?URL=pages/$0.php [L]
Update:
When the request is /cdi/index.php?URL=pages/abc.php
, the second rule extracts the needed URL
part and redirects the client to the new URL path. The client then requests the new URL /cdi/abc
and the third rule takes this and does an internal rewrite to the real content.
This all works fine as it should, but would rewrite and redirect indefinitely. To break this endless rule, the first rule checks the environment %{ENV:...}
, if the request was already redirected REDIRECT_STATUS
and then stops the cycle with the RewriteRule
RewriteRule ^ - [L]
which matches everything ^
and does no substitution, but ends the rewrite cycle with the flag [L]
Instead of using the system provided environment STATUS
/REDIRECT_STATUS
, you can also set a variable yourself with the flag E=SEO:1 for example, and then test for this variable with
RewriteCond %{ENV:REDIRECT_SEO} 1
For the REDIRECT_
prefix, see Available Variables.
You could try this:
RewriteRule ^/([a-z0-9_-]{1,40})/?$ index.php?URL=pages/$1.php
Though ideally you might want to get rid of the "pages/" part of the query string variable, as this fixed constant could be handled by the index.php script.
If you want the group ([0-9]+)
to be alphabetic then just change it to ([a-z]+)
and if you've wanted it to be alphanumeric, then change it to ([a-z0-9]+)
, and ([a-z0-9-_]+)
if with a hyphen and an underscore. If you've wanted it to set their limits manually, you can do that with this format ([a-z0-9-_]{1,40})
. Do you see, the plus sign is gone, for it limited the [chars]
with 1 to anything, and the {1,40}
limited the [chars]
with 1 to 40, you can either change it.
Do you know what the real problem is? Is my stress.. Imagine even I know that you want to remap /$var into /index.php?URL=pages/$var.php I'm still trying giving you a wrong information that will rewrite /index.php?URL=pages/$var.php into /$var. I just have realize that after my 4 hours sleep. Did you see what's happening when the time of your sleep isn't right? Maybe a rule I would gives to you when my brain's in functioning well, was:
RewriteRule ^([a-z0-9-_]+)/?$ /index.php?URL=pages/$1.php
Why did the viewers letting this to happened.. My previous codes are needed to be voted down.
You approach seems fine but your RewriteCond
doesn't match your requirements:
RewriteCond %{REQUEST_URI} ^index.php?URL=pages
means "rewrite the URL if someone requests something that starts with 'index.php"—but that's not what anyone will be requesting. You want your visitors to request pretty URLs.
If your server only needs to serve those requests for /the-page, you can drop the condition entirely. Then any URL will be rewritten. (Note: This might not be what you want!)
Otherwise, the condition should read something like this:
RewriteCond %{REQUEST_URI} ^[a-z0-9-_]{1,40}
If you don't want to mess with regular expressions, you could also try this:
RewriteCond %{REQUEST_FILENAME} !-f
which means "if the user requests a URL for which no file can be found, rewrite the URL according to the upcoming RewriteRule
."