Mod_rewrite forward everything to index.php 's params

给你一囗甜甜゛ 提交于 2019-12-13 02:28:44

问题


How can I get anything but files to rewrite to index.php's params? I'm using apache, mod_rewrite, etc.


回答1:


If you want to rewrite anything that isn't a request for a regular file I'd use this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/index\.php -f
    RewriteRule ^(.*)$  /index.php?page=$1 [QSA]
</IfModule>

This way you don't need to concern yourself about adhering to a certain URL format for your rewrites - you can test and act accordingly in your serving script.




回答2:


Try this:

RewriteRule /([^/]*)/([^/]*)/([^/]*)[^\.].*  /index.php?var1=$1&var2=$2&var3=$3 [R=301]

This will redirect url like /foo/bar/baz to /index.php?var1=foo&var2=bar&var3=baz



来源:https://stackoverflow.com/questions/3740355/mod-rewrite-forward-everything-to-index-php-s-params

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