.htaccess - replacing last hyphen (out of many) with a forward slash [regex?] - 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

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

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

How can I redirect users from a url to another using regex and htaccess?

I want to redirect users from www.example.com/ANYTHING to www.example.com.
Is .htaccess the better way to do it? How can I do it?
If you really just have to remove everything after www.mydomain.com, then you just have to delete everything from the first / to the end of the URL:
Search for this regex
%^([^/]*).*$
and substitute with \1, as I did here. Note that I have used the % sign as delimiter instead of /, so I don't need to escape the / in the regex. (I could have used any other available symbol other than /.)
You'll need to use a mod_rewrite RewriteRule (as opposed to a mod-alias RedirectMatch) in order to avoid conflicts with mod_dir and the DirectoryIndex*1.
For example, in .htaccess (Apache 2.2 and 2.4):
RewriteEngine On
RewriteRule . / [R,L]
The single dot matches something (ie. not the document root) and we redirect to the document root.
However, if the document root is an HTML webpage that links to resources like JavaScript, CSS and images then you need to make exceptions for these resources, otherwise these too will be redirected to the root!
For example:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|jpg|png|gif)$ [NC]
RewriteRule . / [R,L]
*1 A mod_alias RedirectMatch directive such as RedirectMatch /. / ends up matching the rewritten request (by mod_dir) to the DirectoryIndex (eg. index.php) resulting in a redirect loop.

Rewrite URL condition - Change particular string & remove trailing numbers

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

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]