Question

I have always struggled with regex, and after reading up on it for 45 mins my head is spinning. Negative lookaheads, what the...? (?:/(?:(?!s\d+).)*)+$<--- OMG!!!

:(

So, I have a rule

RewriteRule /([0-9]+) /?id=$1 [R]

and it works fine when the url is www.hi.com/123

How can I make it refresh to / (the document root i nthis case) if the url is www.hi.com/123abc or www.hi.com/a123bc?

I just want to make sure only urls with numbers and nothing else are matched.

I tried

RewriteRule /([0-9]+)([^a-z]+) /map.htm?marker=$1 [R]

But that refreshes towww.hi.com/?id=404, oddly enough.

Was it helpful?

Solution

To match 1 or more numbers in regex it will be:

[0-9]+

To match 1 or more numbers or letters in regex it will be:

[0-9a-zA-z]+

For your case RewriteRule rule will be:

RewriteRule ^([0-9a-z]+)/?$ /map.htm?marker=$1 [NC,L,R=302]

which will match /123abc OR /123abc/ OR /123 OR /abc/, note that trailing slash is optional. Flags I used are:

NC    - Ignore Case
L     - Last
R=301 - Use http status 302 for resulting URL

I would strongly suggest you reading mod_rewrite Reference doc: Apache mod_rewrite Introduction

However you also asked:

How can I make it refresh to / (the document root i nthis case) if the url is www.hi.com/123abc or www.hi.com/a123bc?

That rule would be:

RewriteRule ^([0-9a-z]+)/?$ / [NC,L,R=302]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top