Using regex to 301 a directory - wordpress

I'm working with a redirection plugin in Wordpress that accepts regex and I want to 301 an entire directory to something new.
http://www.example.com/tag/exampletag1/ &
http://www.example.com/tag/exampletag2/
to
http://www.example.com/music/exampletag1/ &
http://www.example.com/music/exampletag2/
Is this possible? There are quite a few tags so I don't want to do this manually. Would this be easier to do in the htaccess file?
Anything that will get me going in the right direction is great.

The following ruleset in .htaccess would redirect /tag/ to /music/
RewriteEngine On
RewriteRule ^/tag/(.*) /music/$1 [R=301]
[R=301] indicates an external redirect with HTTP status code 301.

Related

Why I can't redirect subdomain from htaccess while having Cloudflare?

Why I can't redirect subdomain from htaccess while using CloudFlare SSL?
PS I don't know if I can paste URL here so I won't paste it to avoid removal of the question.
I'm getting this problem in Chrome (but it doesn't work in Firefox either)
error
I know there are fixes on the internet for this error but none worked for me. Is it because I have root domain connected to CloudFlare (also affects subdomains)?
What I did is:
1. created a subdomain in hosting and set its file directory.
2. in that file directory made .htaccess file.
3. inside the file
Redirect 301 / https://rootdomain/firstfolder
but it falls into redirects loop :(
With my .htaccess code, I check if the domain has the www. dub on the domain, then I redirect it to the subdomain without the www. dub on it. Also, because I don't think you want to change it, I place a 301 Redirect on it also.
I forgot to mention, usually people do not use the www. on the subdomain also, so I am guessing that you don't want people to use it either.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ "http\:\/\/sundomain\.domain\.com\/" [R=301,L]
For specific url : put below code in your subdomain.domain.com's .htacess
Redirect 301 /blue-car/ http://www.example.com/cars/red-car

"The page isn't redirecting properly" using redirect in .htaccess wordpress

I’m trying to rewrite the url from example.com/sub/sub1 to example.com/sub1 and remove the /sub/ path every time there is a /sub1 next to it.
My solution redirects to example.com/sub1 properly, but I am getting a loop redirect with the error message:
The page isn't redirecting properly.
My .htaccess file:
RedirectMatch 301 /sub(/.*)sub1 $1sub1
Any ideas on how to fix this?
Thanks, guys.
Edit: should be RedirectMatch 301 /sub(/.*)sub1 $1sub1
You should use redirects for specific pages, but if you want to redirect a whole subdirectory, you should be using rewrite rules instead. Using your example, you should have:
RewriteEngine on
RewriteRule ^/$/sub/sub1/(.*)$ /1sub1/$1 [R=301]

301 Redirect not working (WordPress) - [ Probler with ?m=1 in URL ]

I have very weird problem.
My WordPress site gets traffic to the same page with different additional parameters.
Example:
mysite.com/page1.html?m=1
mysite.com/page1.html
It is the same page, but ?m=1 makes WP show 404 error page.
I tried 301 redirect like this (in actual HTACCESS file i also use http:// but here I cannot):
Redirect 301 /page1.html?m=1 mysite.com/page1.html
But this does not do anything.
Traffic comes from google, so I cannot change this URL structure - I have to work with what I got ... So how I fix this???
It could be either WP or HTACCESS problem ... I searched and cannot find anything - i get results for M1 rifle :(
Please help - this is a live site
You can't match against the query string in mod_alias's Redirect or RedirectMatch. You need to use mod_rewrite's %{QUERY_STRING} or %{THE_REQUEST} variables. Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule ^(.*)$ /$1? [L,R=301]
This will redirect any request that has the "m=1" query string and remove it.
.htaccess
RewriteEngine On
RedirectMatch 301 ^/page1.html?m=1$ /page1.html
Try that see also this answer .htaccess 301 redirect of single page

Redirect some .htm page to WordPress page without path and query string

I want to permanent redirect some .htm page from subdomain to main domain WordPress page, for this I am using this code
Redirect permanent /cat/FSBO76.htm http://www.example.com/cat/my-favorite/
But I am getting a problem normally my WordPress page working fine but when I click on old link its redirect with trailing query string and showing page not found error.
http://www.example.com/cat/my-favorite/?cat=FSBO76
Can anyone tell me how can I redirect without path and query string? or any way to solve this problem.
I already try with Redirect 301 also.
EDIT: Its only happening for .htm files, all other files and directory redirecting properly.
This sounds like maybe mod_alias (Redirect) and mod_rewrite is interferring with each other. If you already have wordpress rewrite rules in your htaccess file, you need to stick with mod_rewrite instead of using mod_alias here. Try using something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule ^cat/FSBO76.htm$ http://www.example.com/cat/my-favorite/ [L,R=301]
where "subdomain.example.com" is your subdomain.

.htaccess 301 redirect not working on WordPress site

I'm trying to redirect domain.com/our-team/team-member/ to domain.com/about/
Here is the .htaccess code that I'm using:
RedirectMatch 301 ^/our-team/team-member(.*) ^/about/$1
However what happens with that code is that I'm redirected to http://domain.com/about/team-member/, which is essentially 404 page.
I've tried numerous variations on this htaccess rule with no luck as of yet.
Could something else be at play here? Could this be related to the way WordPress works, or something going on with the shared Go Daddy hosting environment the site is hosted in?
You have a bad target URL starting with ^. Also it is better not to mix mod_alias rules i.e. RedirectMatch with other mod_rewrite rules of WP. Use this rule just below RewriteBase line in your main WP htaccess:
RewriteRule ^our-team/team-member(/.*)?$ /about$1 [L,R=301,NC]

Resources