[英]Removing PHP extension and re-writing query string with .htaccess
我正在尝试重组现有网站,同时将URL重新编写为更用户友好的格式。 我现在拥有的是http://example.com/test_vars.php?test_var1=value1&test_var2=value2
,理想情况下,我想要的是http://example.com/test_vars/test_var1/value1/test_var2/value2
诚然,我是修改htaccess文件的新手,但是已经完成了我的研究并尝试了一些方法。 我最接近的是我要寻找的东西(诚然,距离不是很近),但是我在下面附上了代码:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
DirectoryIndex index.html
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(([^/]+/)*[^.]+)$ /$1 [L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.html
RewriteRule .* index.html
这样,我最终得到的URL为http://example.com/test_vars.php/test_var1/value1/test_var2/value2
但是没有任何变量传递到页面。
在您的test_vars.php
,您必须手动解析参数。 $_SERVER['REQUEST_URI']
将包含/test_var1/value1/test_var2/value2
。 例如,编写一个简单的函数以使用explode()
解析字符串。
要使您的网址不带“ .php”扩展名,请更改此行
RewriteRule ^(([^/]+/)*[^.]+)$ /$1 [L]
对此:
RewriteRule ^test_vars test_vars.php
现在您有了漂亮的URL: http : //example.com/test_vars/test_var1/value1/test_var2/value2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.