Php htaccess重写-localhost中的漂亮URL
我正在本地主机上编写一个PHP脚本,我在.htaccess中有这段代码Php htaccess重写-localhost中的漂亮URL,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,我正在本地主机上编写一个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 Rew
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/index.php?mode=overview&s=10
作为
但是我想访问这个URL
http://localhost/Domain/index.php?mode=overview
http://localhost/Domain/index.php?mode=overview&s=10
作为
我没能得到它:(
任何人都可以帮我解决这个问题!!试试这个:
RewriteRule ^overview/([0-9]+)/$ index.php?mode=$1 [NC,L]
用这段代码替换.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]
您似乎不了解重写和/或正则表达式是如何工作的。这会将
概述/
之后的内容附加到模式=
之后。这绝对不是OP想要的。您不仅在制作概述
固定/静态/必需,而且在模式=
之后放置了错误的内容忽略s=
部分。我刚刚编辑了它,现在如果还没有,请使用两个正斜杠进行尝试,如图所示。我不确定问题到底是什么,现在无法进行测试。您使用的变体应该可以使用^/?(.*)/(.*)$index.php?mode=$1&s=$2[NC,L]
。如果这不起作用,可以始终使用更严格的表达式,例如^/?([a-z]+)/(.*)/?$index.php?mode=$1&s=$2[NC,L]
在将问题发布到此处之前,我尝试了很多解决方案,但没有人对我有效。您的上一个解决方案返回了“内部服务器错误”当删除NC时,我得到的网页没有任何样式。我仍然有同样的问题,这一行“RewriteRule^([^/]+)/?$index.php?mode=$1[L,QSA]”工作正常。但是这一行“RewriteRule^([^/]+)/([^/]+)/?$index.php?mode=$1&s=$2[L,QSA]”不工作。您是否按原样复制/粘贴此代码或已对其进行了修改?不,但网页没有任何样式我复制并按原样粘贴它:(对于css/js路径问题,只需在css、js、图像文件中使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以http://
或斜杠/
开头。您可以尝试将其添加到页面的HTML标题中:
,以便从该URL解析每个相对URL。)d不是当前的URL。