Nit*_*ety 3 apache .htaccess url-rewriting
如果不是现有文件或文件夹,我需要htaccess规则将uri重写为get变量.例:
www.example.com/page1.php -> goto page1.php
www.example.com/page2.html -> goto page2.html
www.example.com/folder -> goto /folder
www.example.com/some_string_value -> rewrite as /default.php?value=some_string_value
Run Code Online (Sandbox Code Playgroud)
这真的是一个非常普遍的要求.在这个网站上可能有1000个关于此问题的问题.您需要使用RewriteCond和REQUEST_FILENAME来查找不存在的文件夹,然后在内部重写以获取变量.基本上如果它是a 404 (non existent URI),它将被路由到你的default.php文件.这就是URL的完成方式.您可以将它放在根目录中的.htaccess文件中.
RewriteEngine On
#prevent the use of the default.php file directly and redirect to friendly URI
RewriteCond %{THE_REQUEST} [A-Z]{3,9}\ /default\.php\?value=([^&\ ]+)
RewriteRule ^ /%1? [R=301,L]
#redirect non existent (404) folder to get variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /default.php?value=$1 [L]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
228 次 |
| 最近记录: |