Beginner's apache mod_rewrite assistance

我们两清 提交于 2019-12-13 03:55:51

问题


I am not really familiar with apache mod_rewrite.

I have url parameters such as {domain}/index.php?blog=5

I simply want to make it {domain}/home.php?client=5

Is it a task as simple as it sounds and can anyone help?


回答1:


The following might work, give it a try

RewriteCond %{REQUEST_URI} ^/home.php [NC]
RewriteCond %{QUERY_STRING} client=([0-9]+) [NC]
RewriteRule (.*) http://%{REMOTE_HOST}/index.php?blog=%1 [L]



回答2:


That seems pretty simple, to be honest — once you get your head into mod_rewrite, it's not that complex.

It sounds like you want to add

RewriteEngine on
RewriteRule ^/index.php?blog=(.+)$ /home.php?client=$1

to your configuration.

Some caveats:

  • If you are putting this in a .htaccess file, then remove the / from the RewriteRule line.
  • If you want to make this case-insensitive, add [NC] to the end of that same line.
  • If you want users to see the URL change (so sending a 302 Found redirection to the browser), then add [R] to the end of the RewriteRule line.
  • If you want both a 302 Found and for the URL to be case-sensitive, combine the two instructions as [NC,R] at the end of the RewriteRule line.

It's definitely worth reading the mod_rewrite docs, but the rule above should be all you need for this use-case.



来源:https://stackoverflow.com/questions/2920816/beginners-apache-mod-rewrite-assistance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!