Redirect using mod_rewrite

喜夏-厌秋 提交于 2019-12-25 19:01:36

问题


Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^haveg/employer/([0-9]+)/(.*) haveg/employer.php?msg_id=$1

It works fine when i use

http://localhost/haveg/employer/7003/the-message-title
or
http://localhost/haveg/employer/7003/

The problem is here http://localhost/haveg/employer/7003 because i removed the forward slash at the end. it gives page not found error.


回答1:


RewriteRule ^haveg/employer/([0-9]+)/?(.*) haveg/employer.php?msg_id=$1

I think adding a questionmark should allow it to match.




回答2:


Try changing your last line to

RewriteRule ^haveg/employer/([0-9]+)([^0-9]*) haveg/employer.php?msg_id=$1

This should accept both cases.




回答3:


I suggest you add another RewriteRule to make your intention clear. In this code the first rule handles the case when the URL ending with digits followed by an optional slash (when the msg_id query field is blank), and the second applies where there is a message following the digits.

RewriteRule ^haveg/employer/([0-9]+)/?$ haveg/employer.php?msg_id=
RewriteRule ^haveg/employer/([0-9]+)/([^/]+)$ haveg/employer.php?msg_id=$1 [L]


来源:https://stackoverflow.com/questions/9066747/redirect-using-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!