301 Redirect Problems - .htaccess

I have a url structure that is passed through as following
www.mysite.com/path1/path2/pageA.html - www.mysite.com/newpath/pageA.html
www.mysite.com/path1/path2/path3/pageB.html - www.mysite.com/newpath/path3/pageB.html
www.mysite.com/path1/pageC.html - www.mysite.com/newpath/pageC.html
I have made the changes for the pass through like this and it is working
RewriteRule ^/newpath/(.*) /path1/path2/$1 [PT,L]
RewriteRule ^/newpath/(.*) /path1/$1 [PT,L]
I have also made changes in code to have the new URL's in pages except for some pages where we need 301 redirects to the new structure
Actual Location
www.mysite.com/path1/path2/pageA.html
www.mysite.com/path1/path2/path3/pageB.html
URL path in hyperlinks that have to be 301 redirected to Final Path URL
www.mysite.com/newpath/path2/pageA.html
www.mysite.com/newpath/path3/pageB.html
Final URL Path
www.mysite.com/newpath/pageA.html
www.mysite.com/newpath/path3/pageB.html
I am not able to create 301 redirects with the current pass through changes present as mentioned above.

You need to remove leading slash from URI matching pattern in .htaccess:
RewriteRule ^newpath/(.+)$ /path1/$1 [NC,L]

Related

How to rewrite URLs that cause duplicate content?

I have a lot of links with a double URL structure. Basically, I need a .htaccess rule that redirects all URLs that start /de (for German language) to the same URL with only a /.
example.com/de/Shop/Tradition/Jagd-Forst/
to
example.com/Shop/Tradition/Jagd-Forst/
Your rewrite rule should match starting with /de/ (with an optional strating slash, capture everything afterwards with (.*) and redirect to the capture group ($1) with a 301 permanent redirect:
RewriteEngine on
RewriteRule ^/?de/(.*) /$1 [R=301,L]

301 Redirection from old archive link to new

I changed my blog archive links from https://example.com/2019/09/ to https://example.com/blog/2019/09/. I would like to redirect from old url to new one. Is there any way to redirect all archive url along with /blog/ slug. Currently old urls goes to 404.
Try the following at the top of your .htaccess file:
RewriteEngine On
# Redirect "/NNNN/NN/" to "/blog/NNNN/NN/"
RewriteRule ^\d{4}/\d\d/$ /blog/$0 [R=302,L]
The $0 backreference contains the entire URL-path that is matched by the RewriteRule pattern (1st argument).
Change the 302 (temporary) redirect to 301 (permanent) only once you have confirmed that it works as intended.
Assuming this is WordPress then you don't need to repeat the RewriteEngine On directive, since it probably already occurs later in the file.

Redirecting Urls from old website to New

I am trying to redirect my website from old domain to new domain using htaccess. Both the websites have same pages with same url structure i only need to redirect the main url part i-e https://test.com old url to https://test1.com with other pages url as well. For example if i had a page in old website (https://test.com/page1) it should be redirected to (https://test1.com/page1). I used this code to redirect it but its not working any help.
Redirect 301 /page1/ https://test1.com/page1
Redirect 301 https://test1.com
You might find these helpful:
#Redirect from old domain to new domain
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
and
#Redirect from old domain to new domain with full path and query string:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]
Feel free to follow the resource: https://gist.github.com/ScottPhillips/1721489
The first Redirect has a different source and target URL-path. The old URL has a trailing slash and new one has not.
Then any request beginning with URL-path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-path will be appended to the target URL.
This means, if you request
http://old.example.com/page1/some/script.php
the path beyond /page1/ (some/script.php without leading slash) will be taken and attached to the target /page1
http://new.example.com/page1some/script.php
To have the complete path appended, you must omit the trailing slash in the old URL-path, e.g.
Redirect /page1 https://test1.com/page1
To address the second Redirect, this will only work inside a Location directive, e.g.
<Location "/page1">
Redirect https://test1.com/page1
</Location>
Finally, when everything works as it should, you may change the status code to 301. Never test with 301.

301 redirect subfolder while maintaining trailing URL

I need to redirect a section of my website which has a new subfolder name
currently my website looks like the following...
www.website.com/old-folder/product-one
www.website.com/old-folder/product-two
But I need to change the subfolder name, the rest of the URL which contains a number of products is remaining the same see below.
www.website.com/new-folder/product-one
www.website.com/new-folder/product-two
I tried the below which redirects all the URLS included in /old-folder to /new-folder correctly but it sends hem all to the root of /new-folder rather than their unique path.
RewriteRule ^old-folder/(.*)$ /new-folder/(.*)? [L,R=301]
Any Help is appreciated, thank you.
You need to use a $1 to backreference what you've matched:
RewriteRule ^old-folder/(.*)$ /new-folder/$1 [L,R=301]
You can also use mod_alias:
RedirectMatch 301 ^/old-folder/(.*)$ /new-folder/$1

301 redirect after url rewrite

I did some htaccess URL rewrite. To keep my google ranking I must redirect the old URL to the new one; the problem is the old URL still 'exist' and I'm not sure how to do the redirect. This is an example:
old url: mypage.php?id=myId
which now is rewritten as: mypage-myId.html
this is the htaccess directive
RewriteRule ^mypage-([A-Za-z0-9_-]+).html$ mypage.php?id=$1 [L]
now I want to 301 redirect all the old url (mypage.php?id=myIds) to the new url (mypage-myIds.html).
I tried this at the top of my htaccess file:
redirect 301 mypage.php?id=1 to mypage-1.html
but nothing happens, the page stays on mypage.php?id=1.
What's wrong with this? I found another post about this problem
url rewrite & redirect question
but the solution wasn't that clear to me.
Thanks in advance
Vittorio
You could try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([A-Za-z0-9_-]+)$ # fetch ID
RewriteRule ^mypage\.php$ http://domain.com/mypage-%1.html [R=301,L] # redirect old URL to new
RewriteRule ^mypage-([A-Za-z0-9_-]+)\.html$ mypage.php?id=$1 [L] # rewrite

Resources