htaccess Redirect 301 directory - .htaccess

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.

Related

htaccess redirect repeats folder in URL over and over?

I'm trying to redirect camping.website.com to website.com/camping. I have an .htaccess file under the subdomain on my server with this rule:
Redirect 301 / https://website.com/camping/
With the slash on the end like above, the URL it returns is:
https://website.com/camping/camping/camping/camping/camping/camping/camping/
If I remove the slash on the end, it will return this instead:
https://website.com/campingcamping
If I change the destination URL to anything else, the redirect works normally. It's only when I have the word "camping" that it messes up. Is there some kind of conflict because the folder I'm trying to redirect to is the same as the subdomain?
Edit: I should mention something else. The following redirect:
Redirect 301 /category/camping-parks/ https://website.com/camping/camping-parks/
Returns this:
https://website.com/campingcamping/camping-parks/
Strange, huh?
Your problem is within this rule :
Redirect 301 / https://website.com/camping/
Will catch both domain and subdomain requests as you commented that they are in same server.
Try this using mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?camping\.website\.com [NC]
RewriteRule ^(.*)$ https://website.com/camping/$1 [L,R=301]
Note: clear browser cache then test

Remove directory from URL with

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]

How to Permanent Redirect a Directory That No Longer Exists (via .htaccess)

I am having trouble redirecting an entire directory that no longer exists on our server.
All variations of the following are not working and I simply get a 404 Page Not Found.
.htaccess file looks like this:
redirect 301 /non_existent_directory/ http://my.website.com/existent_directory/
Is it possible to use the Redirect 301 directive for this? Or is this only solvable by mod_rewrite?
Thanks
I even tried:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?my\.website\.com\/non_existent_directory\/$ [NC]
RewriteRule ^(.*)$ http://my.website.com/existent_directory/ [R=301,L]
No luck...
From the Redirect documentation, I would say
Redirect 301 /non_existent_directory http://my.website.com/existent_directory
or
Redirect 301 /non_existent_directory /existent_directory
should work, provided you are allowed to use this in a .htaccess file. See also Troubleshooting .htaccess files. You should test without 301 though, to prevent caching of bad redirects by the client.
If this doesn't work, you can try a RewriteRule of course
RewriteEngine On
RewriteRule ^/?non_existent_directory(.*)$ /existent_directory$1 [R,L]
But this is equivalent to the above Redirect directive.

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

Redirect from old indexed pages with url rewrite

I have some old indexed pages in Google that need to be redirected to their new locations. Example: Google shows wwww.domain.com/pages/subpages/page.php that is now in www.domain.com/pages/page.php.
I also need, when someone clicks on the old page in Google, to open rewritten URL.
Example: User clicks on wwww.domain.com/pages/subpages/page.php, it will open him wwww.domain.com/pages/page.php, but with www.domain.com/page in address bar (without "pages/" and php extension).
So I have written this .htaccess code
Redirect 301 /pages/subpages/page.php http://www.domain.com/page
RewriteEngine On
RewriteRule ^page/?$ /pages/page.php
The redirect works, but user have in address bar full URL (http://www.domain.com/pages/page.php), not the rewritten one(http://www.domain.com/page). It looks like the mod_rewrite doesn't work with redirect or something.
Is there any solution?
This code works fine for me:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# 301 redirect
RewriteRule ^pages/subpages/page\.php$ http://www.domain.com/page [R=301,L]
# rewrite
RewriteRule ^page/?$ /pages/page.php [L]
Please ensure that you have no other redirects/rewrites in your htaccess file as they may may perform additional rewrite/redirect actions.

Resources