.htaccess append folder name to end of URLs - regex

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.

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

How to check for dot(.) in mod_rewrite

I want to redirect URL
domain/Family_He..
to
domain/Family_Health_insurance
using RewriteRule. I have tried with
RewriteRule /Family_He(.*)$ /Family_Health_insurance
and it is working. But I have some more page with urls like
domain/Family_Health_info
domain/Family_Health_quote
domain/Family_Health_child etc
When I tried as
RewriteRule /Family_He\.\.$ /Family_Health_insurance
then this won't works for me. Please help me out.
Are you aware that there are actually two spaces at the end of your subject string that would prevent \.$ (a literal dot at the end of the string) from matching?
To have it redirect (302) you need to add the R flag. To match the dot you need to escape it using \. like
RewriteRule ^/Family_He\.\.$ /Family_Health_insurance [R=302,L]
Note that the leading slash doesn't work in htaccess, only in httpd.conf

.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.

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