【发布时间】:2020-01-15 06:56:32
【问题描述】:
我正在尝试使我的网站中的 url 对用户友好,但似乎服务器根本没有考虑 .htaccess 重写命令。
该项目位于localhost 中,名为ecommerce。
我想“转换”这个网址:localhost/ecommerce/product?name=abc
到localhost/ecommerce/product/abc。
这是 .htaccess 文件:
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
##hide /index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\index [NC]
RewriteRule ^ %1 [R,L,NC]
##HERE IS THE IMPORTANT PART
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /product\.php\?name=([^&]+)
RewriteRule ^ /product/%2/? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?product/([^/]+)/?$ /product.php?name=$1 [L]
当我访问localhost/ecommerce/product?name=abc 时,网址保持原样,当我访问localhost/ecommerce/product/abc 时,我得到500 Internal Server Error。
我以为xampp httpd.conf有问题,所以改了
<Directory />
AllowOverride None
Order allow,deny
Allow from all
</Directory>
到
<Directory />
AllowOverride All
Order allow,deny
Allow from all
</Directory>
还是不行!
从 .htaccess 中删除我隐藏 .php 扩展名的部分也没有任何区别。
【问题讨论】:
-
这个.htaccess 是否位于
/ecommerce/目录中? -
@anubhava 是的,在
localhost/ecommerce/里面 -
将
RewriteBase /更改为RewriteBase /ecommerce -
@Gulshan 谢谢,但这也行不通。
标签: php .htaccess url mod-rewrite xampp