简体   繁体   English

重写URL在.htaccess文件中不起作用

[英]Rewrite URL doesn't work in .htaccess file

I have problem using .htaccess to rewrite some of the friendly URL. 我有问题使用.htaccess来重写一些友好的URL。

The root URL is http://www.example.com/my/ 根URL是http://www.example.com/my/

The working call url is http://www.example.com/my/Post.php?id=2 工作电话网址是http://www.example.com/my/Post.php?id=2

Both .htaccess file and Post.php are located in the sub-directory http://www.example.com/my/ .htaccess文件和Post.php都位于子目录http://www.example.com/my/

But I wanted it to be like the following: http://www.example.com/my/job/kuantan/?id=2 但我希望它如下: http//www.example.com/my/job/kuantan/?id = 2

The .htaccess configuration as below: .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]

BUT, once i deployed the above .htaccess configuration file, it prompted 500 internal error message as well. 但是,一旦我部署了上述.htaccess配置文件,它也会提示500内部错误消息。

Is something missing or wrong in the .htaccess configuration? .htaccess配置中是否缺少或错误? Please advice and really appreciated if someone there could give a hand. 如果有人可以伸出援助之手,请提出意见并表示非常感谢。 Thanks. 谢谢。

You can have this code in /my/.htaccess : 您可以在/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]

Query String is automatically carried over so there is no need to capture it in rule. 查询字符串会自动转移,因此无需在规则中捕获它。

You should just need a single rule: 你应该只需要一个规则:

RewriteRule ^/my/job/kuantan/$ /my/Post.php

The query string is carried over by default. 查询字符串默认是转义的。

Assuming that .htaccess and Post.php are located in the sub-directory /my/ : 假设.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]

Remove all spaces between [] . 删除[]之间的所有空格。 Change /Post.php?id=1 to Post.php?id=$1 . 更改/Post.php?id=1Post.php?id=$1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM