Remove directory from URL with - .htaccess

I am moving a Magento site from example.com/shopping to example.com and I have to 301 redirect all of the URLs to the new location (example.com). I am assuming I can use mod_rewrite in the .htaccess file to create a rewrite rule to do this? I tried to learn how to use mod_rewrite, but the syntax is so complex for me.
There are hundreds of pages that need to be redirected. Example:
http://www.example.com/shopping/product-category-1/product-sub-category.html
TO
http://www.example.com/product-category-1/product-sub-category.html
This is so that people don't end up with a 404 error when they enter the site via an old URL.

Try adding this to the htaccess file in your document root, preferably above any rules you may already have there:
RewriteEngine On
RewriteRule ^shopping/(.*)$ /$1 [L]
If you want to redirect the browser so that the new URL appears in the location bar, add an R flag in square brackets:
RewriteEngine On
RewriteRule ^shopping/(.*)$ /$1 [L,R=301]

Related

how to put a pattern based generic redirect in .htaccess

I have 2000+ old URLs like
example.com/hello/public/news
example.com/hello/public/notifications
example.com/hello/public/results
now I have trimmed hello/public this part in the new version
so how do I write a generic redirect in .htaccess so if I get hit on these
it redirects to
example.com/news
example.com/notifications
example.com/results
and more ...
I suspect you will already have some mod_rewrite directives in your .htaccess file, in which case you do not need to repeat the RewriteEngine On directive.
Try the following at the top of your .htaccess file:
RewriteRule ^hello/public/(.*) /$1 [R=302,L]
Test first with 302 (temporary) redirect before changing to a 301 (permanent).

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]

htaccess Redirect 301 directory

I've read many things about this, but none seems ok at the moment.
I've to redirect:
http://mysite.it/255-dir-name/
to:
http://mysite.it/forum/255-dir-name/
Also, everything which is in this directory must be redirected to the new path.
I've tried with this, but doesn't work:
RewriteRule ^255-dir-name/(.*)$ /forum/255-dir-name/$1 [R=301,NC,L]
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^255-dir-name(.*) http://example.it/forum/255-dir-name/$1 [R,L]
Once you are satisfied that the redirect works, you can change the R to R=301 to make it permanent.
Warning! When you test the rewrite, do so in a NEW browser, or fully clear its cache. That is because your previous 301 redirect has been cached by your browser, so your browser does not even request the page from your server anymore. Instead, it requests the previous rewrite directly.
You can redirect the first url to the second url with the following directives in your .htaccess:
RewriteEngine on
RewriteRule ^([0-9]+-[^/]+/?)$ forum/$1 [R,L]
This will redirect any url that starts with one or more numbers, a dash, and then one or more non-slash characters to the forum-url.

Problems with htaccess Redirect (Page on one domain to same page on another)

I'm trying to redirect a few pages to a new domain and I have done this before but for some reason I can't get the code to work.
RewriteEngine On
Redirect 301 http://domain.com/page1.html http://domain2.com/page1.html
Can anyone see where I'm going wrong?
In .htaccess file the below code will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain.
The .htaccess file needs to be placed in the root directory of your old website
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Since you say you only want to direct a FEW of your pages and not all of them, you can do:
RewriteEngine On
Redirect /~myaccount/oldpage.html http://www.newsite.com/newpage.html
You specify the path to the page on your current server followed by the URL to redirect to.
OR you can do:
RedirectMatch 301 ^/oldpage\.html$ http://www.newsite.com/newpage.html

Resources