htaccess: Mediafire.com like urls
I'm trying to come up with some mod_rewrite to translate http://example.com/?7gu开发者_运维知识库dznrxdnu
into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact
and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L]
with [L,R=301]
.
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond
) the query string consists solely of a-z
and/or 0-9
, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=
, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]
精彩评论