删除php文件扩展名,并将_getparam设置为目录格式
我知道这个问题被问了很多次,但我没有得到适合我的情况的正确解决方案,可能是因为我对这个代码缺乏了解 到目前为止,我使用以下代码隐藏php文件的扩展名:删除php文件扩展名,并将_getparam设置为目录格式,php,apache,.htaccess,mod-rewrite,url-rewriting,Php,Apache,.htaccess,Mod Rewrite,Url Rewriting,我知道这个问题被问了很多次,但我没有得到适合我的情况的正确解决方案,可能是因为我对这个代码缺乏了解 到目前为止,我使用以下代码隐藏php文件的扩展名: Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] 当我浏览像这样的url时:http://example.com/solutionsforhttp://example.com
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
当我浏览像这样的url时:http://example.com/solutionsforhttp://example.com/solutions.php
它工作得很好
但是当我想使用带有\u GET
的url时,像这样:http://example.com/solutions/page_slug
其中page\u slug
是\u GET
参数say?slug=page\u slug
。。。然后,此时它会生成404
notfounderror
,因为它将页面段塞视为solutions目录中的文件
因此,我希望它应该将url中的第一个参数视为文件名,然后最多将两个参数视为get param,例如:http://example.com/solutions_file/param1_page_slug/param2_any_other_param_may_be_id
如果有两个以上的参数,则url结构将是正常的,例如,如果url如下所示:
http://example.com/solutions_file/param1_page_slug/param2_any_other_param_may_be_id/param3_this_is_extra
那么我们的url结构如下:
http://example.com/solutions_file?param1=page_slug¶m2=any_other_param_may_be_id¶m3=this_is_extra¶m4=and_so_on
这就是我要实现的目标,我如何实现这一目标?您需要为每一个目标制定一个特定的规则:
Options -MultiViews
RewriteEngine On
# for just the php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)$ /$1.php [L]
# for 1 param
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ /$1.php?slug=$2 [L,QSA]
# for 2 params
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /$1.php?slug=$2¶m2=$3 [L,QSA]
# for 3 params
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /$1.php?slug=$2¶m2=$3¶m3=$4 [L,QSA]
等等。感谢这项工作,对于3个以上的参数,我可以手动传递查询字符串。非常感谢:)