Rewrite URL condition - Change particular string & remove trailing numbers - regex

What's the best way to rewrite URLs as 301 redirects with the following conditions?
Sample old URLs to rewrite:
/c/garments-apparel/red-yellow-polka-dress-10_450
/c/shoes-and-accessories/black-suede-boots-02_901
Conditions:
Change c to category
Remove trailing number (including connecting dash) from URL (example: -10_450 and -02_901)
New URLs should be:
/category/garments-apparel/red-yellow-polka-dress
/category/shoes-and-accessories/black-suede-boots
Note that changes will be applied to an .htaccess file on a Wordpress environment.

You can have this rule just below RewriteEngine On line:
RewriteEngine On
RewriteRule ^c/([\w-]+/.+)-[\d_]+/?$ /category/$1 [L,NC,R=301]

you can use the regex
[-_]\d+
to replace the trailing numbers with "" (empty string) demo
then use the regex
\/c\/
and replace with /category/ demo

Related

Rewrite URL condition - Remove category in URL and trailing numbers

What's the best way to rewrite URLs as 301 redirects with the following conditions?
Sample old URLs to rewrite:
/products/garments/red-yellow-polka-dress-00519
/products/shoes/black-suede-boots-02508
Conditions:
Change word products to product
Remove category from URL (example: /garments and /shoes)
Remove trailing number (including connecting dash) from URL (example: -00519 and -02508)
New URLs should be:
/product/red-yellow-polka-dress
/product/black-suede-boots
Note that changes will be applied to an .htaccess file on a Wordpress environment.
You can have this rule just below RewriteEngine On line:
RewriteEngine On
RewriteRule ^products/[\w-]+/(.+)-\d+/?$ /product/$1 [L,NC,R=301]

.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

Unknown number of regex replacements, how?

I need to change a large number of URIs in the following way:
substitute %20 separators with dashes -,
substitute the old root with a new domain.
Examples:
/old_root/first/second.html -> http://new_domain.com/first/second
/old_root/first/second%20third.html -> http://new_domain.com/first/second-third
/old_root/first/second%20third%20fourth.html -> http://new_domain.com/first/second-third-fourth
The best I came up with using regex is to write as many pattern-replacement rules as the maximum number of %20 separators that can occur in my URIs:
old_root/(.*?)/(.*?)\.html -> http://new_domain.com/$1/$2
old_root/(.*?)/(.*?)%20(.*?)\.html -> http://new_domain.com/$1/$2-$3
old_root/(.*?)/(.*?)%20(.*?)%20(.*?)\.html -> http://new_domain.com/$1/$2-$3-$4
My question is: is it possible to obtain the same result using a single regular expression rule?
I thought I could use a two-step approach: first change all %20 separators to - and then use the rule old_root/(.*?)/(.*?)\.html -> http://new_domain.com/$1/$2/. However, I need to apply this rule in a .htaccess file as a RedirectMatch directive and, as far as I know, it is not possible to use two successive rules for the same redirect directive.
It turns out that Apache recursively applies all regex rules until they stop matching. Therefore, I am allowed to write two rules rather than one to solve my problem.
The following rules do what I was looking for, and more; I have tested them on my local Apache server and they work fine. Note that for them to work, you need to first turn on the rewrite engine by prepending
RewriteEngine on
Options +FollowSymlinks -MultiViews
in the local .htaccess file or in the global httpd.conf file.
Replace all spaces with hyphens
Replace both literal spaces and %20 with hyphens:
RewriteRule ^(.+)(\s|%20)(.+)$ /$1-$3 [R=301,NE,L]
Replace all apostrophes with hyphens
Replace all literal apostrophes and %60 with hyphens:
RewriteRule ^(.+)('|`|%60)(.+)$ /$1-$3 [R=301,NE,L]
Delete the trailing .html extension
RewriteRule (.+)\.html$ $1 [R=301,L]
Convert the last field in the URL to lower case
Convert the last field in the URL to lower case and prepend the new domain:
RewriteRule /whatever/(.*?)/(.*?)/(.*) http://new.domain.com/$1/$2/${lc:$3} [R=301,L]
Important: The lowercase conversion will only work if you include the following lines at the end of the Apache configuration file httpd.conf, which is usually located in the etc directory on the server:
RewriteEngine on
RewriteMap lc int:tolower
A last note: I recommend prepending each rule with a RewriteCond directive to restrict the field of application of the rule. For example, to apply the space-to-hyphens rule only to those URI that match a certain regex, you should write the following in your .htaccess file:
RewriteCond %{REQUEST_URI} regex_for_URIs
RewriteRule ^(.+)(\s|%20)(.+)$ /$1-$3 [R=301,NE,L]
where regex_for_URIs is the regular expression that the URI must match in order for the next RewriteRule to be applied; it can also be a simple string.
Well, you were almost done.
Problems
Don't return "%20" - We'll Use them as "delimiter" of parts of the path
Add condition on third & fourth group (because you might pass short URL i.e. your examples)
Solution
\/old_root\/(.*?)\/(\w*)(?:%20)?(\w*)?(?:%20)?(\w*)?\.html
See Demo
Explanation
(?:%20)? means "%20" is non catching group that can occurs 0 or 1 time.
Same logic applyies on possible 3rd & 4th part.

.htaccess rewrite rule for adding a trailing slash by unmatching a string in the URL

The following .htaccess rule un-matches string admin and adds a trailing slash(/) to that URL if admin is not found in the URL
RewriteRule ^((?!admin).)*((?!\/).)$ /$1/ [L,R]
But it has an error, and it is
http://www.domain.com/index
should result to :
http://www.domain.com/index/
But currently it is resulting:
http://www.domain.com/inde/
Please find a solution to correct it.
Thanks Very much .
Your expression captured the last character in a group.
This will solve the issue:
RewriteRule ^(?!.*admin)(.*?)\/?$ /$1/ [L,R]
Check out the explained demo here: http://regex101.com/r/kL6pV1
Note: this will invalidate any URL that contains admin, not necessarily starting with admin

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