htacess mod_rewrite regex - include numbers but exclude letters - regex

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.

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]

Related

htaccess Rewrite rule for anything after the domain

I've got a one page website that just has a switch that replaces some content with a local town names.
The normal domain.com will go to index.php, but I want anything after the domain to be passed in the GET query.
So domain.com/nottingham would go to index.php?town=nottingham
I have around 300 town names, so I dont ant to hardcode them all so I'm trying to get the $1 to pass the value and it doesn't seem to work.
I've got the following in my .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9])$ /index.php?town=$1 [NC,L]
ErrorDocument 404 /index.php
But when I try domain.com/townname I get "Warning: Undefined array key "town" in..." So I assume thats missing my rewriterule and gonig to the 404.
You forgot to include a quantifier in your pattern - [a-zA-Z0-9] means allow for one character out of this character group. And since you anchored your pattern at the start and end, it would match when you requested domain.com/t, but not with any path component longer than that.
Add the + quantifier after the character group, to say "a character matching this group, one or more times":
RewriteRule ^([a-zA-Z0-9]+)$ /index.php?town=$1 [NC,L]

.htaccess - replacing last hyphen (out of many) with a forward slash [regex?]

I have xxxx's of URLs which are in the following format:
http://www.example.com/sub/worda-wordb-wordc-123456789
However I have external links to my site with the URLs in the following format:
http://www.example.com/sub/worda-wordb-wordc/123456789
I'd like to redirect all URLs from
http://www.example.com/sub/worda-wordb-wordc/123456789
to
http://www.example.com/sub/worda-wordb-wordc-123456789
Please try the following:
RewriteEngine On
# Redirect URI with last slash, replacing with hyphen
RewriteRule ^sub/([\w-]+)/(\d+)/?$ /sub/$1-$2 [R=302,L]
Here, we are checking for letters, digits, underscores, and hyphens with ([\w-]+), digits with (\d+) and an optional slash on the end with /?, just to be sure, and then redirecting it accordingly.
Be sure to make this one of your first rules, and then change 302 to 301 to make the redirect cached by browsers and search engines.
You can use this .htaccess file:
RewriteBase /
RewriteRule ^sub/(.*)/([0-9]+)$ /sub/$1-$2
Now if you go to http://www.example.com/sub/worda-wordb-wordc/123456789 the url will be rewritted to http://www.example.com/sub/worda-wordb-wordc-123456789.
If this is not what you were looking for please add more details to your question.
You can use this rule in your site root .htaccess:
RedirectMatch 301 ^(sub)/(.*)-(\d+)/?$ /$1/$2/$3

.htaccess redirect not working as expected

I am trying to redirect the following uris: search, search/acquia-search , search/acquia-search/, search/site or search/site/ to the homepage which is /. Here is my rewrite rule.
RewriteRule ^(search|search/acquia-search|search/site)(?!.*) / [R=301,L]
In the above rule I am trying to match those uris and ensure that they're not followed by anything using the negative lookahead.
here is my full .htaccess http://pastebin.com/stNgzfnD
what am I doing wrong?
Edit: The above did not work but the following did:
RewriteRule ^(search|search/acquia-search|search/site)/?$ / [L,R,NC]
(?!.*) negative lookahead doesn't make any sense since it will always fail the regex.
Better use this regex:
RewriteRule ^search(/(acquia-search|site/?)?$ / [L,R,NC]

Regular Expression for Mod rewrite via HTACCESS

I need a regular expression which I can use in an HTACCESS file to rewrite:
http://www.sample.com/dir/1-2-3.php
1 = lower case letters only, no limit on how many
2 = alpha numeric (lower case letters only) and dashes, no limit on how many characters
3 = alpha numeric (upper case letters only), no limit on how many characters
(NOTE: The dashes between 1, 2, 3 are intentional, and will be present in the URL)
to
http://www.sample.com/dir/sub/page.php?v=ABC12345
Where ABC12345 is #3 from the original URL.
If I'm understanding correctly, the following should work.
RewriteEngine On
RewriteRule ^([a-z]*)-([a-z0-9-]*)-([A-Z0-9]*)\.php /$1/$2.php?v=$3 [L]
Hope this helps.
Try this rule in your .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^dir/([a-z]*)-([a-z0-9-]*)-([A-Z0-9]*)\.php$ /dir/sub/page.php?v=$3 [R=301,L,NE,QSA]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
QSA flag will make sure to append existing query parameter with additional query parameters
$3 is 3rd capture group in your REQUEST_URI

Help with Regex to match and rewrite URI

I need to have a RegEx that will match a URI like this based on the subdomain "blog"--
http://blog.foo.com/2010/06/25/city-tax-sale/
and redirect like this (getting rid of the subdomain and numbers/date)--
http://foo.com/city-tax-sale/
where the last bit "city-tax-sale" would be a wildcard. So basically any incoming URI that starts with 'blog.foo.com' would be redirected to 'foo.com' + 'whatever is at the end of the above URI after the three sub paths with numbers.
I hope that makes sense. Just trying to create one redirect instead of writing every single one.
This will explicitly match your date format, rather than any series of digits and slashes:
RewriteCond %{HTTP_HOST} ^blog\.foo\.com$ [NC]
RewriteRule ^/\d{4}/\d{2}/\d{2}/(.*)$ http://foo.com/$1 [L,R=301]
The regex part can be broken does to:
^ # start of non-domain url
/\d{4} # slash followed by 4 digits
/\d{2} # slash followed by 2 digits
/\d{2} # slash followed by 2 digits
/ # closing slash
(.*) # rest of the url, captured to group 1
$ # end of url
With the $1 in the replacement being group 1.
In the options part:
L is for "Last" - tells it to not bother looking at other rules.
R=301 is for Redirect with 301 header, which means permanent redirect (just R would send a temporary 302 header)
The RewriteCond bit performs a case-insensitive (NC option) check on the HTTP_HOST header (supplied by user/client) and if it starts blog.foo.com it performs the rewrite, otherwise it doesn't.
RewriteCond %{HTTP_HOST} ^blog.foo.com [NC]
RewriteRule ^(\d+/)+(.*)/?$ http://foo.com/$2 [L,R=301]
You can try this:
/http:\/\/blog\..*\.[a-zA-Z]{2,5}\/[0-9]{4}\/[0-9]{2}\/[0-9]{2}\/(.*)\//