Redirect using mod_rewrite

2019-09-20 01:28发布

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.

3条回答
老娘就宠你
2楼-- · 2019-09-20 01:43

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

I think adding a questionmark should allow it to match.

查看更多
可以哭但决不认输i
3楼-- · 2019-09-20 02:02

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]
查看更多
叛逆
4楼-- · 2019-09-20 02:07

Try changing your last line to

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

This should accept both cases.

查看更多
登录 后发表回答