开发者

.htaccess and mod_rewrite help

Using mod_rewrite, how could I turn URLs that follow thi开发者_开发技巧s pattern:

http://example.com/index.php?id=14

Into this pattern:

http://example.com/14/


As I think that you rather want to rewrite requests to /<ID>/ internally to /index.php?id=<ID>, try this:

RewriteRule ^(\d+)/$ index.php?id=$1

But if you really want to redirect requests to /index.php?id=<ID> internally to /<ID>/:

RewriteCond %{QUERY_STRING} (^|&)id=(\d+)(&|$)
RewriteRule ^index\.php$ /%2/? [R]

And if you want to preserve possible other URL arguments:

RewriteCond %{QUERY_STRING} ^(([^&]*&+)*)id=(\d+)(&+(.*))?$
RewriteRule ^index\.php$ /%3/?%2%5 [R]


RewriteCond %{QUERY_STRING} id=(.+)
RewriteRule ^/index.php$ /%1/? [R]


Your code is the only one worked finally in my .htaccess file, however I have the second querystring and couldnt get it working. My keys are id and name. How can i get

http://example.com/studio_details.php?id=123&name=Studio Wrox London

to

http://example.com/123/Studio Wrox London
(here in particular I might have a problem as there are spaces in the names from the database - can this be resolved with a - or get it working with space fine?)

  1. The below is what I have in the .htaccess file which is on a shared hosting server from a service provider. How can i add more querystrings to the following?

RewriteCond %{QUERY_STRING} ^(([^&]&+))id=(\d+)(&+(.*))?$ RewriteRule ^studio_details.php$ /%3/?%2%5 [R]

  1. How can I rewrite all .php extensions that exist in my website to .html? As none of the rewrites worked in mycase. (only above in point 1)

  2. Eventhough I dont have any code in my .htaccess file I get my index.php file redirected to the host directory such as "vhost\websites\syildiz\www..." etc. Is there any rule for making sure there the index page is index.php (which will be rewritten to index.html)

  3. is there a rule to check if the rule works before executing it? like checking if the file and the querystring variables executed and resulted before displaying the page?

thanks Suls

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜