Forum Moderators: phranque
I need them to be opened after requests like:
example.com/?m=20081212
example.com/?page=12
example.com/?p=673
I tried lots of variant but all the attemps were not successfull. The code listed below is not working too. :(
RewriteEngine on
RewriteCond %{QUERY_STRING} ^.*$
RewriteRule ^(.*)$ $1.htm [L]
Please help me.
If the filename is index.htm and is defined in DirectoryIndex and .htm files are able to be parsed for PHP (or whatever it is that you use) scripts, then you don't need any sort of rewrite rules to make the URL example.com/?x=y connect to the file index.htm using parameters x=y with that file.
Where you would use a RewriteRule, is to make a URL like example.com/page/12 connect to an internal file at /index.php?page=12. That's the more normal usage of such a rewrite.
Is that what you want to do?
Maybe I should use some %{QUERY_STRING} rule?
Now I've got a .htaccess file that allows to open pages like example.com/p=123
and it would be nice if I could open this file by typing
example.com/?p=123
in a browser.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^$ %1.htm [L]
Jim
Something like this would be safer:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^([a-z]+=[a-z0-9.]+)$ [NC]
RewriteRule ^$ %1.htm [L]
Jim
So, further hardening the code, we end up with
RewriteEngine on
RewriteCond %{QUERY_STRING} ^([a-z]+=[a-z0-9.]+)$ [NC]
RewriteRule ^$ [b]/%1[/b].htm [L]
Thanks,
Jim
but it occasionally seems to cause trouble for others (maybe due to incorrect DocumentRoot definitions?)
It shouldn't because Apache needs an URL-path for an internal redirect (
ap_internal_redirect()). Problems should be a problem of the redir processing. If your don't supply an URL-path directly in the substitution (nor an URL of course), mod_rewrite will build an URL-path either via replacing the per-dir prefix with the URL-path from the directive RewriteBase or by stripping the DocumentRoot. The later one fails when the request was not translated invoking the DocumentRoot (Alias, various ways of vhost mass hosting etc). However, supplying an URL-path will stop mod_rewrite's loop detection-abort-function (because this is based on r->filename, the full physical path).