
[英]rewrite pretty urls with php and htaccess, mapping 404 for subpages
[英]PHP .htaccess Rewrite - Pretty URLs in localhost
我正在通过本地主机处理PHP脚本,并且在.htaccess中有此代码
RewriteEngine On
RewriteBase /Domain
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php?mode=$1 [L]
此代码使我可以访问此URL
http://localhost/Domain/index.php?mode=overview
如
http://localhost/Domain/overview
但是我想访问这个URL
http://localhost/Domain/index.php?mode=overview&s=10
如
http://localhost/Domain/overview/10
我没有得到它:(
请任何人都可以帮助我!
用此代码替换.htaccess文件的最后一行应该可以解决问题。
^/(.*)/(.*)$ index.php?mode=$1&s=$2
我没有测试过,但是应该可以。 如果有效,这就是原因:
它在基本URL后寻找一个单词,在您的情况下, http://localhost/Domain/
是基本URL。 此匹配“ ^(。 )”在缓冲区1中捕获,称为“ $ 1”。 然后,它需要一个正斜杠,然后在URL末尾添加另一个捕获字“(。 )$”。 此匹配位于捕获缓冲区2“ $ 2”中。 如果找到此匹配项,则使用适当的捕获缓冲区将其重写为index.php。
您可以使用以下代码:
RewriteEngine On
RewriteBase /Domain/
#Make sure it's an actual file
RewriteCond %{REQUEST_FILENAME} -f [OR]
#Make sure its a directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#Rewrite the request to index.php
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?mode=$1&s=$2 [L,QSA]
#Rewrite the request to index.php
RewriteRule ^([^/]+)/?$ index.php?mode=$1 [L,QSA]
尝试这个:
RewriteRule ^overview/([0-9]+)/$ index.php?mode=$1 [NC,L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.