我在使用.htaccess重写一些友好的URL时遇到问题。
根URL为http://www.example.com/my/
工作呼叫url为http://www.example.com/my/Post.php?id=2
.htaccess文件和Post.php都位于子目录中http://www.example.com/my/
但我希望它是这样的:http://www.example.com/my/job/kuantan/?id=2
.htaccess配置如下:
DirectoryIndex index.php
Options All -Indexes
#ErrorDocument 404 /404.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^job/kuantan/([0-9]+) /Post.php?id=$1 [L,QSA, NC]
但是,一旦我部署了上面的.htaccess配置文件,它也会提示500内部错误消息。
.htaccess配置中是否缺少或出错?请提供建议,如果有人能伸出援手,我将不胜感激。谢谢
您只需要一条规则:
RewriteRule ^/my/job/kuantan/$ /my/Post.php
默认情况下,查询字符串会继续。
假设.htaccess和Post.php位于子目录/my/
:中
DirectoryIndex index.php
Options All -Indexes
#ErrorDocument 404 /404.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^job/kuantan/([0-9]+)$ Post.php?id=$1 [L,QSA,NC]
删除[]
之间的所有空格。将/Post.php?id=1
更改为Post.php?id=$1
。
您可以在/my/.htaccess
:中拥有此代码
DirectoryIndex index.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /my/
RewriteCond %{THE_REQUEST} /Post'.php [NC]
RewriteRule ^ job/kuantan [R=302,L,NE]
RewriteRule ^job/kuantan/?$ Post.php [L,NC]
查询字符串是自动结转的,所以不需要在规则中捕获它。