htaccess 将 uri 重写为 get if not file/folder
htaccess to rewrite uri into get if not file/folder
我需要 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
这真是一个非常普遍的要求。在这个网站上可能有 1000 个关于这个问题的问题。您需要使用 RewriteCond
和 REQUEST_FILENAME
来查找不存在的文件夹,然后在内部重写以获取变量。本质上,如果它是 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= [L]
我需要 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
这真是一个非常普遍的要求。在这个网站上可能有 1000 个关于这个问题的问题。您需要使用 RewriteCond
和 REQUEST_FILENAME
来查找不存在的文件夹,然后在内部重写以获取变量。本质上,如果它是 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= [L]