Help with Regex to match and rewrite URI - regex

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}\/(.*)\//

Related

redirect all requests starting with 0x to index.html

My website will be like example.com/0xETHEREUMADDRESS.
So i want to redirect all those request starting with 0x to INDEX.HTML and index.html has already the code to the rest of work.
I need .htaccess code to redirect all starting with 0x to index.html.
Here is my tried .htaccess rules file.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule (0x*)$ /index.html [L,R=302]
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule (0x*)$ /index.html [L,R=302]
The regex (0x*)$ matches URLs that end-with 0, 0x, 0xx, 0xxx etc. It does not match URLs that start-with 0x, so this will not match the desired URL. The * character is a regex quantifier that repeats the preceding token 0 or more times. There is also no need for the capturing group (surrounding parentheses).
If the rule only matches URLs that start with Ox then the condition that checks the URL is not /index.html is therefore redundant.
The following will do what you are asking:
RewriteRule ^0x /index.html [R=302,L]
The ^ is the start-of-string anchor, so the requested URL must start with 0x. Note that the URL-path matched by the RewriteRule pattern does not start with a slash.
However, I'd question whether you really want to "redirect" the user? (As in an external HTTP redirect - which is what this is.) Redirecting will lose the original URL and expose /index.html to your users.
Internal "rewrite" instead
If, however, you wish to internally rewrite the request instead so that index.html can analyse the requested URL (as you say, "index.html has already the code to the rest of work") and keep /0xETHEREUMADDRESS in the browser's address bar then remove the R=302 flag and the slash prefix on the substitution string. For example:
RewriteRule ^0x index.html [L]
Reference:
https://httpd.apache.org/docs/current/rewrite/intro.html#regex

Using htaccess to redirect anything after a parameter - regex

Id like to have the following URL(s) redirect to the same URL just without the ?
For example:
https://www.example.com/this-is-static?numbersletterssymbols
goes to
https://www.example.com/this-is-static
"numbersletterssymbols" can be anything
Id like this to be a 301 , using htaccess ( apache )
I came across the following, however, the variable seems to be in parentheses
RewriteCond %{QUERY_STRING} ^product=(.*)$
RewriteRule ^test.php$ %1/? [R=301,L]
Any insight is appreciated
To remove the query string (any query string) from any URL you could do the following using mod_rewrite, near the top of your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI} [QSD,R=301,L]
The condition (RewriteCond directive) simply asserts that there is a query string consisting of at least 1 character (determined by the regex . - a single dot).
The QSD (Query String Discard) flag removes the original query string from the redirected response. The QSD flag requires Apache 2.4 (which you are most probably using). The method used on earlier versions of Apache, as in your example, is to append a ? to the susbstitution string (essentially an empty query string).
Note that you should test first with a 302 (temporary) redirect to avoid potential caching issues.
however, the variable seems to be in parentheses
The parentheses in the regex simply creates a "capturing group" which can be referenced later with a backreference. eg. In your example, the value of the product URL parameter is referenced in the RewriteRule substitution string using the %1 backreference in order to redirect to the value of the URL parameter. This is very different to what you are trying to do and is arguably a security issue. eg. It would redirect a request for /test.php?product=https://malicious.com to https://malicious.com/, allowing a potential hacker to relay traffic via your site.
UPDATE: is it possible to make this work only for when the URL begins with "this-is-static" (for example)
Yes, the RewriteRule pattern (1st argument) matches the URL-path, less the slash prefix. For example:
RewriteCond %{QUERY_STRING} .
RewriteRule ^this-is-static %{REQUEST_URI} [QSD,R=301,L]
Matches all URLs that start with /this-is-static.

RewriteRule redirect URL with query string in htaccess

I've no experience with regex, and the redirect rule I'm trying to put in my .htaccess file for a WordPress site is having no effect.
I want to redirect:
https://example.com/example/?person=name
to
https://example.com/example/people/name
From reading, I figure my rule ought to be:
RewriteRule \?person=(.*) https://example.com/example/people/$1 [R=301,L]
What am I missing/doing wrong?
From reading, I figure my rule ought to be:
RewriteRule \?person=(.*) https://example.com/example/people/$1 [R=301,L]
You can't match the query string part of the URL using the RewriteRule pattern. The RewriteRule pattern matches against the requested URL-path only. To match the query string you need to use a condition and check the QUERY_STRING server variable.
For example:
RewriteCond %{QUERY_STRING} ^person=([^&]*)
RewriteRule ^example/$ /example/people/%1 [QSD,R=302,L]
This needs to go before the existing WordPress directives.
This matches the URL-path exactly as stated in your question, ie. /example/.
%1 (as opposed to $1) is a backreference to the last matched condition, ie. the value of the person URL parameter, that occurs as the first URL parameter.
The QSD (Query String Discard) flag (Apache 2.4+) is required to remove the query string from the target URL. If you are still on Apache 2.2 then append an empty query string (ie. append a ?) to the end of the subsitution string instead.

htacess mod_rewrite regex - include numbers but exclude letters

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]

.htaccess regex, redirect to new folders

I'm trying to redirect some old files/folders that used to follow this pattern:
foldername/filename.extension
foldername has:
(2010 or 2011)(text A-Z a-z 0-9 _ or -)
filename has:
(text A-Z a-z 0-9 _ or -).extension
Ex: 2011aug_SomeNameHere/image.jpg
The new folder tree organizes the files by year (so everything is one level deeper):
2010/foldername/filename.extension
and
2011/foldername/filename.extension
Ex: 2011/2011aug_SomeNameHere/image.jpg
And I have the following for my rewriterule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^http://(www\.)?domain.com/path/to/+(2010|2011)+([A-Za-z\_\-])+/([A-Za-z0-9\_\-]\.([A-Za-z]))$ http://domain.com/path/to/$1/$1$2/$3 [L,R=301]
</IfModule>
In Firefox I'm getting a redirect to:
domain.com/path/to/2011aug_SomeNameHere/image.jpg/2011aug_SomeNameHere/image.jpg/2011aug_SomeNameHere/image.jpg...
In Chrome I'm getting a 404 error with the url at:
domain.com/path/to/2011aug_SomeNameHere/image.jpg
Does anyone have any ideas/tips?
Try this:
RewriteRule ^path/to/(2010|2011)([A-Za-z\_\-]+)/([A-Za-z0-9\_\-]+\.[A-Za-z]+)$ /path/to/$1/$1$2/$3 [L,R=301]
It is not required to use have the domain in your rewrite unless you are handling multiple domains with one rewrite. Your first group being $1 would have been your (www.) if you went to your domain with www infront. I suggest you look up your use of +'s and groups to understand more of the regex. Having your +'s outside the groups allows for matching of the same group more than once. In your case you want to match all of it in one group. You also had a nested group for your file extension match where it wasn't need.