.htaccess regex, redirect to new folders - regex

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.

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 : Redirect (301) thousands of Url's containing directories to simple url's

I need to convert with HTACCESS method tons of URL's allready produced (and already indexed...) by Wordpress for articles containing folders/subfolders to simples URL's without any folder/subfolder name.
Example:
FROM https://www.website.com/Animals/Cats/mycat.html TO https://www.website.com/mycat.html
FROM https://www.website.com/Animals/Dogs/mydog.html TO https://www.website.com/mydog.html
FROM https://www.website.com/Countries/France/bordeaux.html TO https://www.website.com/bordeaux.html
etc...
I already changed permalinks options in Wordpress config. So, now URL's produced are in the good format (Ex: https://www.website.com/bordeaux.html) without any folder name.
My problem is to redirect all OLD Url's to this new format to prevent 404 and preserve the rank.
If tryed to add in my .htacess this line :
RewriteRule ^/(.*)\.html$ /$1 [R=301,L,NC]
I egally tryed RedirectMatch 301 (.*)\.html$ method and it's the same. I'm going crazy with this.
What am i doing wrong and could you help me?
Thanks
RewriteRule ^/(.*)\.html$ /$1 [R=301,L,NC]
The URL-path matched by the RewriteRule pattern never starts with a slash. But you can use this to only match the last path-segment when there are more "folders". And the target URL also needs to end in .html (as per your examples).
So, this can be written in a single directive:
RewriteRule /([^/]+\.html)$ /$1 [R=301,L]
This handles any level of nested "folders". But does not match /foo.html (the target URL) in the document root (due to the slash prefix on the regex), so no redirect-loop.
(No need for any preceding conditions.)
Here the $1 backrefence includes the .html suffix.
Just match the last part of the url and pass it to the redirect:
RewriteRule /([^/]+)\.html$ /$1.html [R=301,L,NC]
It will match any number of directories like:
https://www.website.com/dir1/page1.html
https://www.website.com/dir1/dir2/page2.html
https://www.website.com/dir1/dir2/dir3/page3.html
https://www.website.com/dir1/dir2/dir3/dir3/page4.html

.htaccess append folder name to end of URLs

We have a multi language site with product URLs in English like:
example.com/en/product-name
example.com/en/product-name2
Product names can have any latin character, number and dashes in them.
Due to a restructuring, all English URLs need to have -en appended to them like so:
example.com/en/product-name-en
example.com/en/product-name2-en
Can anyone help me to find RewriteCond and RewriteRule to match any URL with /en/ in it and 301 redirect to the same URL with a -en appended at the end?
Try something like the following, using mod_rewrite, near the top of your .htaccess file:
RewriteEnging On
# Append "-en" to URL if it starts "/en/."
RewriteCond %{REQUEST_URI} !-en$
RewriteRule ^en/[\w-]+$ %{REQUEST_URI}-en [R=301,L]
It is preferable to first test with a 302 (temporary) redirect in order to avoid caching issues.
\w is a shorthand character class that matches a-z, A-Z, 0-9 and _ (underscore), to which we add the - (hyphen) to the character class. (I assume you mean "hyphen", when you state "dashes" - although these are strictly different characters.)
The preceding condition (RewriteCond directive) is to avoid redirecting a URL that already ends with -en. The ! prefix negates the regex.

.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

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