Change name of subfolder and redirect - .htaccess

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]

Related

Redirect htaccess part of url

I want to create a sort of wildcard to redirect to a new url.
I replaced a specific part, that should be redirected.
All url's that contain the following /computers/notebooks-laptops/
Should be redirect to /computers/laptops/
So the following URL
https://www.example.com/computers/notebooks-laptops/product1
should become:
https://www.example.com/computers/laptops/product1
How can I achieve that?
I am assuming you are using the .htaccess in root directory, use below rule.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/computers/notebooks-laptops/(.*)
RewriteRule ^ /computers/laptops/%1 [R=301,L]

How can I redirect everything in one sub directory using htaccess

I have a website and need everything in my subdomain to redirect to my home page.
For example I would need www.website.com/sub to redirect back to www.website.com
same with anything else within /sub
so www.website.com/sub/sdjdj/sdsd/dsd ....would also need to redirect to www.website.com
How can I achieve this with htaccess?
You can use:
RewriteEngine on
RewriteRule ^sub(?:/|$) / [NC,R,L]
You can use this in your .htaccess in the root.
RewriteEngine On
RewriteRule ^sub(?:$|/.*) / [R=302,L]
When you are sure the rule is working as intended, change to 301 in rule.

301 Redirect a folder to a single page

I have a folder on my website that has been superceded and I want to redirect an attempt to access any file in this folder to the current home page.
I have read many questions here but none seem to do exactly this.
I have tried various things in .htaccess but it seems to always append the filename to the redirect address.
redirect 301 old/$ www.example.com/index.html
redirect 301 old/ www.example.com/index.html
redirect 301 ^old/ www.example.com/index.html
I have seen various mentions of RewriteRule. Do I need to use that?
You can use this code in your /old/.htaccess file:
RewriteEngine On
RewriteRule ^ http://www.example.com/index.html [L]
OR from root .htaccess use:
RewriteEngine On
RewriteRule ^old(/.*)?$ http://www.example.com/index.html [L,NC]

Check url for string and 301 redirect to url without the string

I have checked a lot of htaccess redirects but I can't seem to find one that suits my needs. We have an old url like this:
www.domain.com/shoes/nike/filter/gender/men
I would like a rule that checks the url for the string 'filter' and 301 redirects it to:
www.domain.com/shoes/nike
Is that possible with htaccess?
This should help you:
RewriteEngine On
RewriteRule (.*)/filter http://www.domain.com/$1 [R=301,L]
It simply deletes everything after "filter"

How to redirect these old urls to new ones (subfolder) via htaccess

I am a newbie and would like to redirect all my old urls to new ones (moved as subfolder) via htaccess. Also, with 301 permanent redirect.
Old url pattern looks like:
http://www.mydomain.com/de/myoldpage.html
New url pattern should be:
http://www.mydomain.com/shop/de/myoldpage.html
I am using the same domain and same page urls but only my shop is moved to a subfolder "shop".
How can i write a redirect rule to redirect all the urls in this pattern.
Thanks in advance.
Try this code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^(de/.*)$ /shop/$1 [L,R=301,NC]
RewriteEngine On
RewriteRule ^de/(.*)$ shop/de/$1 [L,R]

Resources