301 redirect subfolder while maintaining trailing URL - .htaccess

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

Related

Rewrite url to another domain while maintaining the last part of url

I have a question similar to this
URL rewrite for part of domain name
Tried to use that rule but it didn't work. I think the url rule could be different.
I have the url:
olddomain.com/test/ref/s/page
olddomain.com/test/ref/s/page1
olddomain.com/test/ref/s/page2
I need to rewrite them and maintain the last part of the url, eg:
https://www.newdomain.com/test/ref/s/page
https://www.newdomain.com/test/ref/s/page1
https://www.newdomain.com/test/ref/s/page2
Edit:
The ending of the url is dynamic so using individual redirection for each link is not practical.
Much thanks for any help.
You can use your .htaccess file to do this, similar to the answer you are referencing. If you just want to send your old urls to your new urls individually, you can achieve this quite easily using the Redirect 301 function inside .htaccess, and uploading to the root of "old domain", like so:
Redirect 301 /test/ref/s/page https://www.newdomain.com/test/ref/s/page
Redirect 301 /test/ref/s/page1 https://www.newdomain.com/test/ref/s/page1
Redirect 301 /test/ref/s/page2 https://www.newdomain.com/test/ref/s/page2
This will individually 301 the index at each of the old urls to the corresponding new url.
If you want to redirect everything from "old domain" to "new domain" just use a rewrite like so:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
You would, of course, need to configure this depending on your use of SSL.

Change name of subfolder and redirect

I have some links like this:
http://example.com/firstfolder/oldfolder/file1.html
http://example.com/firstfolder/oldfolder/file2.html
http://example.com/firstfolder/oldfolder/file3.html
http://example.com/firstfolder/oldfolder/file4.html
Now, the oldfolders name has changed to newfolder.
I want to rewrite and redirect all oldfolder to newfolder.
Means, if you open e.g.
http://example.com/firstfolder/oldfolder/file3.html
it should redirect to
http://example.com/firstfolder/newfolder/file3.html
I tried the following:
RewriteEngine On
RewriteRule ^oldfolder/(.*) /newfolder/$1 [R=301,NC,L]
Unfortunately this is doing nothing. There's no redirect. It's the same as before.
What am I doing wrong?
You can use Redirect directive to redirect your old folder uris to the new one :
Redirect 301 /firstfolder/oldfolder/ http://example.com/firstfolder/newfolder/
I find it odd that there is a "."in your url.
Otherwise in the htaccess you can try to add a "\" like this :
RewriteEngine On
RewriteRule ^old\.folder/(.*) /newfolder/$1 [R=301,NC,L]

301 Redirect .htaccess - convert URL ending in .html to same but convert to directory name without ending in .html

I would really appreciate help with the following 301 Redirect using Apache .htaccess as I am stuck on how to do this:
I need to convert:
http://blog.domain.com/2015/03/filename.html
to:
http://www.domain.com/blog/filename/
There are many to do from the original file structure. The year and month that make up the original URL, change and go back each month and for several years. i.e. 2015/03, 2015/02, 2015/01, 2014/12 etc...
The ending filename.html needs to be the name of the new directory name without the ending .html and adding a closing '/'
This is what I have so far, but it doesn't work:
RewriteRule http://blog.domain.com/([0-9]+)/([0-9]+)/(.*)\.html$ http://www.domain.com/blog/$3 [R=301,L]
You can't include the http://domainname/ part of the URL in the regex of a rewrite rule, only the 2015/03/filename.html part is used for any matching.
You could try using mod_alias instead:
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2}/([^/.]+)\.html$ http://www.domain.com/blog/$1/
Or if you already have rewrite rules, better to stick with mod_rewrite:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule [0-9]{4}/[0-9]{2}/([^/.]+)\.html$ http://www.domain.com/blog/$1/ [L,R=301]

Redirect all urls which contain certain parameters to another url which follows a certain pattern

Unfortunately I didn't get it solved by myself and need to ask for help. I want to redirect all urls which follow a certain pattern (in this case it contains "reviews/category"). These URLs supposed to be redirect to another url which is made up the first one:
http://[product-url]/reviews/category/[category-url]
supposed to be redirect to
http://[product-url].html
Furthermore it shouldn't matter if you call the url with or without www.
Example:
http://example.com/ford-blues/reviews/category/cars supposed to be redirect to http://example.com/ford-blues.html
Any help would be much appreciated.
Following code isn't working
RewriteEngine On
RewriteRule ^reviews/category/?$ $1\.html [R=301,L]
Try:
RedirectMatch 301 ^(.*)/reviews/category/ /$1.html
in the htaccess file in your document root.
Or using mod_rewrite:
RewriteEngine On
RewriteRule ^(.*)/reviews/category/ /$1.html [L,R=301]

Help with 301 redirect (Simple concept, per directory control)

I'm trying to get a set of 301 redirects working for a client site, and I'm getting a bit stuck.
I have about 17 different directories that have .html content fileswhich I am moving across to Wordpress. What I am trying to do is create a simple, single .htaccess file with redirects in that will allow me to redirect all the .html files within a directory to wordpress URLs without an extension:
Manual redirect example:
redirect 301 /blog/2009/04/post1.html /blog/2009/04/post1/
redirect 301 /blog/2009/04/post2.html /blog/2009/04/post2/
Trying to create a rule like:
RewriteRule ^/blog/2009/04/(.*?).html$ /blog/2009/04/$1/ [R=301,NC,L]
Where the $1 is the filename (minus the .html) suffixed with a /
I can't determine whether the ^/blog/2009/04/(.*?).html$ means it will only redirect for this particular directory, and will redirect to the right location.
I would obviously want to duplicate this ReWriteRule for all my designated content directories...
Can anyone point me in the right direction?
Thanks,
Lee
Yes, your rule will only redirect for the 2009/04 directory. I think you want:
RewriteRule ^/blog/([0-9]+)/([0-9]+)/(.*?).html$ /blog/$1/$2/$3/ [R=301,NC,L]
This will now also redirect /blog/2008/12/post.html to /blog/2008/12/post/ and so on. Although since you have almost exactly the same format, maybe this will be easier?
RewriteRule ^/blog/(.*?).html$ /blog/$1/ [R=301,NC,L]

Resources