URL Rewriting Help

早过忘川 提交于 2019-12-11 16:05:58

问题


I'm trying to learn Regex & URL Rewriting in PHP. How can I create an .htaccess file which will rewrite (not redirect) any url to index.php?q={url}?

For example:

http://www.example.com/baloon

to

http://www.example.com/index.php?q=baloon

I tried:

RewriteEngine On
RewriteRule ^$ index.php?q=$1 [R]

...but it is not working. What am I doing wrong?

Thanks.


回答1:


Rewriting ANY url to index.php?q=$1 would result in internal server error as it'd create an endless loop; instead do something like this:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php?q=$1 [L]


来源:https://stackoverflow.com/questions/1660563/url-rewriting-help

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