开发者

Apache rewrite rule - regex help

开发者 https://www.devze.com 2023-03-22 19:32 出处:网络
I have 7 urls: url: /v/3b89441db7986135e5eb9e1debf0cc23 url: /v/3b89441db7986135e5eb9e1debf0cc23/login url: /v/3b89441db7986135e5eb9e1debf0cc23/logout

I have 7 urls:

url: /v/3b89441db7986135e5eb9e1debf0cc23
url: /v/3b89441db7986135e5eb9e1debf0cc23/login
url: /v/3b89441db7986135e5eb9e1debf0cc23/logout
url: /v/3b89441db7986135e5eb开发者_如何转开发9e1debf0cc23/access
url: /v/3b89441db7986135e5eb9e1debf0cc23/delete-attendee/:id
url: /v/3b89441db7986135e5eb9e1debf0cc23/edit-attendee/:id
url: /v/3b89441db7986135e5eb9e1debf0cc23/finalise

How could I write a rewrite rule that if these URLS are not matched, I redirect the user to another domain?

For example the part after /v/ is always a 32 character MD5 string. the :id part is always a number.

If you could give me a regex (regex has alas never been my forte) example for

/v/3b89441db7986135e5eb9e1debf0cc23

and

/v/3b89441db7986135e5eb9e1debf0cc23/edit-attendee/:id

that would be excellent.


This regex matches your urls:

/v/[a-f0-9]{32}(/[a-z-]+(/\d+)?)?

In english...

  • /v/ is a literal
  • [a-f0-9]{32} means 32 hex digits
  • /[a-z-]+(/\d+)? means "/" then at least 1 of (any lowercase letter or a dash) then "/" then some digits
  • surrounding a regex in (...)? means either one or none of them

FYI, this regex matches all urls given in question reasonably tightly

If you want to not match, use this:

^(?!/v/[a-f0-9]{32}(/[a-z-]+(/\d+)?)?$)


How about this:

\/v\/[a-f0-9]{32}(\/(login|logout|access|delete-attendee\/:\d+|edit-attendee\/:\d+|finalise)?

This will only match your accepted urls. You should adjust for your flavor or regex and appropriate escape chars.


You said that you want to redirect if URLs do not match, so first you must make a rule that matches all valid URLs, then prepend ! to negate the match.

RewriteEngine On
RewriteRule !^v/[0-9a-f]{32}(/(login|logout|access|delete-attendee/\d+|edit-attendee/\d+|finalise))?$ http://your-other-domain/ [R]

The above rule should be placed in the .htaccess file present in the root directory of your website.

0

精彩评论

暂无评论...
验证码 换一张
取 消