.htaccess RedirectMatch 301 pattern - .htaccess

I have three patterns that I want to redirect in .htaccess.
https://example.com/questions/name-of-question
https://example.com/category/name-of-category
https://example.com/a-4-digit-number/name-of-post
(The No. 3 like: https://example.com/5485/name-of-post)
all of them need a /blog/ after the domain:
https://example.com/blog/questions/name-of-question
https://example.com/blog/category/name-of-category
https://example.com/blog/a-4-digit-number/name-of-post
I used the code below for the first one, once it worked and then something happened and the htaccess got deleted. Now that I use it againg it does not redirect:
RewriteEngine on
Options +FollowSymLinks
RedirectMatch 301 https://example.com/questions/(.*) https://example.com/blog/questions/$1
Can anyone help me with these redirects? Specially the no. 3 with the 4-digit pattern.

Use RewriteRule directive like this to target all the 3 rules into one pattern:
Options +FollowSymLinks
RewriteEngine on
RewriteRUle ^/?(questions|category|\d{4})/.+$ /blog/$0 [L,NC,R=301,NE]

Related

How can i do the dynamic .htaccess redirection on multiple directories level

I have multiple cities directories, want to do the 301 internal redirections for many cities.
e.g.
https://wwww.exampl.com/ciytname/abc/initial_url.html want to redirect on https://wwww.exampl.com/cityname/abc/final_url.html
cityname is variable
my code below not working out.
Redirect 301 /*/abc/initial_url.html /*/abc/final.html
Redirect 301 /cityname/abc/initial_url.html /cityname/abc/final.html
I can't do for each city
For your shown samples, could you please try following.
RewriteEngine ON
RewriteRule ^ciytname/abc/initial_url.html/?$ cityname/abc/final_url.html [NC,L]
For any generic URIs could you please try following.
RewriteEngine ON
RewriteRule ^([\w-]+)/([\w-]+)/initial_url.html/?$ $1/$2/final_url.html [NC,L]
You may use a RedirectMatch rule that supports regular expressions like this:
RedirectMatch 301 ^/([^/]+/abc)/initial_url\.html$ /$1/final_url.html

redirecting to a different directory using htaccess

I'm trying to redirect the following
/books/categories/mountain-literature/my-father-frank.html
to
/books/categories/baton-wicks/my-father-frank.html
This is the line in my .htaccess
RedirectMatch 301 /mountain-literature(.*) /books/categories/baton-wicks$1
it rewrites the url and appends this pageUrl stuff on which I don't want and the correct page doesn't load ?
books/categories/baton-wicks/my-father-frank.html?pageUrl=books/categories/mountain-literature/my-father-frank&contentType=html
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^mountain-literature/(.*)$ /baton-wicks/$1 [L,R=301]
try this or this
Redirect 301 /mountain-literature /baton-wicks
If you dont need the rewrite rule to match multiple URL's, try just hardcoding the target URL, i.e.
Options +FollowSymLinks
RewriteEngine On
RewriteRule mountain-literature/(.*) /books/categories/baton-wicks/my-father-frank.html [L,R=301]
By adding the [L] flag it should tell apache not to allow any other rules to append to the URI

How to redirect urls starting with numbers

What will be a 301 redirect rule using htaccess to redirect urls similar to
domain.com/community/783-anystring-string/profile
to
domain.com/community/
or
any url matching this type of format
domain.com/community/123-anystring-string/...
to
domain.com/community/
Basically I want to redirect any urls domain.com/community/(starting with numbers)-string...
to domain.com/community/
You'll want to use mod_rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^community/[0-9]* /community [R=301,L]
I think. That may not work, I can't test it right now :/

htaccess redirect exact match, exclude all query strings

I'm trying to redirect http://domain.com/search to http://domain.com
However I also have urls that looks like http://domain.com/search?q=someword
At the moment I have :
redirectMatch 301 http://domain.com/search$ http://domain.com
And even though it redirects correctly the http://domain.com/search url, it also redirects any http://domain.com/search?q=someword url to http://domain.com/?q=someword, which I do not want.
What am I doing wrong?
I would suggest you to use mod_rewrite instead of mod_alias in your htaccess :
RewriteEngine On
RewriteBase /
RewriteRule ^search/?$ / [L,R=301]
Add a ? to the end of your target:
RedirectMatch 301 ^/search$ http://domain.com/?
This, however, will cause a ? to appear at the end of the URL in the browser's location bar. If you use mod_rewrite instead, you won't see the ?:
RewriteEngine On
RewriteRule ^/?search$ /? [L,R=301]

How to not pass parameters in 301 redirect?

I want to redirect the following two links:
/catalog/yogicchai/rooibos-masala-chai-naturally-caffeine-c-84.html?infoBox=5 (category link)
/catalog/yogicchai/rooibos-masala-chai-naturally-decaffeinated-p-291.html (product link)
To:
yogicchaiDOTcom/rooibos-masala-chai-naturally-decaffeinated.html
I thought this was the solution:
RedirectMatch 301 /catalog/yogicchai/rooibos-masala-chai(.*)\.html
yogicchaiDOTcom/rooibos-masala-chai-naturally-decaffeinated.html
But the end results is:
yogicchaiDOTcom/rooibos-masala-chai-naturally-decaffeinated.html?infoBox=5
I dont want the "?infoBox=5" printed at the end of the URL above
How can I prevent that from happening?
Simply add a question mark to the target URL like this:
RedirectMatch 301 /catalog/yogicchai/rooibos-masala-chai(.*)\.html
yogicchaiDOTcom/rooibos-masala-chai-naturally-decaffeinated.html?
There cannot be two question marks in an url, so apache will not add the parameters from the previous page, because there is already one in the new.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/yogicchai/rooibos-masala-chai.*?\.html$ /rooibos-masala-chai-naturally-decaffeinated.html? [L,R=301,NC]

Resources