301 redirect syntax for htaccess
-
I'm working on some htaccess redirects for a few stray pages and have come across a few different varieties of 301s that are confusing me a bit....Most sources suggest:
Redirect 301 /pageA.html http://www.site.com/pageB.html
or using some combination of:
RewriteRule + RewriteCond + RegEx
I've also found examples of:
RedirectPermanent /pageA.html http://www.site.com/pageB.html
I'm confused because our current htaccess file has quite a few (working) redirects that look like this:
Redirect permanent /pageA.html http://www.site.com/pageB.html
This syntax seems to work, but I'm yet to find another Redirect permanent in the wild, only examples of Redirect 301 or RedirectPermanent
Is there any difference between these? Would I benefit at all from replacing Redirect permanent with Redirect 301?
-
There is no difference between "Redirect 301", "Redirect permanent" and "RedirectPermanent". It is clear from mod Alias documentation:
"This directive makes the client know that the Redirect is permanent (status 301). Exactly equivalent to
Redirect permanent
." "permanent - Returns a permanent redirect status (301) indicating that the resource has moved permanently."But, these directives are really confusing, because they are not page to page, but directory to directory. For example:
Redirect 301 /a-very-old-post/ http://yoursite.com/a-very-new-post/
Surprisingly, it will redirect all old subpages to new subpages. In particular it will redirect /a-very-old-post/page1 to /a-very-new-post/page1 Therefore better to use RedirectMatch or RewriteCond+RewriteRule for page by page redirections and for redirections with query strings.
Links to docs: https://docs.oracle.com/cd/B14099_19/web.1012/q20206/mod/mod_alias.html
Link to simple RedirectMatch page by page redirects generator: RedirectMatch generator for htaccess https://www.301-redirect.online/htaccess-redirectmatch-generator
Link to good RewriteRule generator: htaccess 301 redirect rewrite generator https://www.301-redirect.online/htaccess-rewrite-generator
-
In **apache **"permanent" "RedirectPermanent" is the same as "Redirect 301"
By default, the "Redirect" directive establishes a 302, or temporary, redirect.
If you would like to create a permanent redirect, you can do so in either of the following two ways:
- Redirect 301 /oldlocation http://www.domain2.com/newlocation
- Redirect permanent /oldlocation http://www.domain2.com/newlocation
Page to Page 301 Redirect Generator for Htaccess
https://www.aleydasolis.com/htaccess-redirects-generator/
If no <var>status</var> argument is given, the redirect will be "temporary" (HTTP status 302). This indicates to the client that the resource has moved temporarily. The <var>status</var> argument can be used to return other HTTP status codes:
<dl> "permanent" & "Redirect 301"</dl>
<dl>
<dd>Returns a permanent redirect status (301) indicating that the resource has moved permanently.</dd>
"temp"</dl>
<dl>
<dt>Returns a temporary redirect status (302). This is the default.</dt>
"seeother"</dl>
<dl>
<dd>Returns a "See Other" status (303) indicating that the resource has been replaced.</dd>
"gone"</dl>
<dl>
<dd>Returns a "Gone" status (410) indicating that the resource has been permanently removed. When this status is used the <var>URL</var> argument should be omitted.</dd>
</dl>
**https://httpd.apache.org/docs/2.4/mod/mod_alias.html **
https://www.bruceclay.com/blog/how-to-properly-implement-a-301-redirect/
To 301 Redirect a Page:
RedirectPermanent /old-file.html http://www.domain.com/new-file.html
To 301 Redirect a Page:
Redirect 301 /old-file.html http://www.domain.com/new-file.html
https://i.imgur.com/PTEj5ZF.png
https://www.aleydasolis.com/htaccess-redirects-generator/
Single URL redirect
Permanent redirect from pageA_.html_ to pageB.html.
.htaccess:
301 Redirect URLs.
Redirect 301 /pageA.html http://www.site.com/pageB.html
https://www.aleydasolis.com/htaccess-redirects-generator/page-to-page/
<ifmodule mod_rewrite.c="">RewriteEngine On
Redirect 301 /pageA.html /pageB.html</ifmodule>https://www.htaccessredirect.net/
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.com[nc]
RewriteRule ^(.*)$ http://www.site.com/$1 [r=301,nc]//301 Redirect Old File
Redirect 301 /pageA.html /pageB.htmlYou asked about Regex
https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules
.htaccess
Regular expressions
Rewrite rules often contain symbols that make a regular expression (regex). This is how the server knows exactly how you want your URL changed. However, regular expressions can be tricky to decipher at first glance. Here's some common elements you will see in your rewrite rules, along with some specific examples.
- ^ begins the line to match.
- $ ends the line to match.
- So, ^folder1$ matches folder1 exactly.
- . stands for "any non-whitespace character" (example: a, B, 3).
- * means that the previous character can be matched zero or more times.
- So, ^uploads.*$ matches uploads2009, uploads2010, etc.
- ^.*$ means "match anything and everything." This is useful if you don't know what your users might type for the URL.
- () designates which portion to preserve for use again in the $1 variable in the second string. This is useful for handling requests for particular files that should be the same in the old and new versions of the URL.
See for more regex
- http://perldoc.perl.org/perlre.html#Regular-Expressions
- https://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet/
- https://www.askapache.com/htaccess/
Hope this helps
Tom
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
Switched from and HTTPS to HTTP. My home page is facing a redirect issue from the http to https. Should I no index the HTTP or find the redirect and delete it? Thank you
Switched from and HTTPS to HTTP. My home page is facing a redirect issue from the http to https. Should I no index the HTTP or find the redirect and delete it? Thank you
Technical SEO | | LandmarkRecovery20170 -
301 Domain Redirect And Old Domain to a New one including pages
Hi, I need to 301 an old domain to a new one (new website) I need to 301 the domain to a new page not the new domain direct for example www.olddomain.co.uk to www.newdomain.co.uk/pagenew Then I need to also 301 all the other pages on the old domain to the new one for example... www.oldmain.co.uk/oldpage to www.newdomain.co.uk/newpage Issue is I can do one or the other not both, I can get the other pages to redirect but then the main domain wont redirect to the correct new page. Or I can get the old domain to redirect but not the internal pages. Thanks
Technical SEO | | David-Sharpe0 -
Help Setting Up 301 Redirects from Coldfusion Site to Wordpress Site.
I have created a new website and need to redirect all of the previous pages to the new one. The old website was built in coldfusion and the new site is built in wordpress. One of the pages I'm trying to redirect is www.norriseal.com/products.cfm to http://norrisealwellmark.com/products/. This is what I have in my .htaccess file <ifmodule mod_rewrite.c="">Options +FollowSymlinks
Technical SEO | | MarketHubb
RewriteEngine On
RewriteBase /
Redirect 301 /products.cfm http://norrisealwellmark.com/products/</ifmodule> The result of this redirect is http://norrisealwellmark.com/products.cfm How do I prevent the .cfm from appending to the destination URL?1 -
Console: Change Of Address, 301 Redirects Step Not Working
Hi everyone, We just made a switch to a new domain based on a rebrand, and we are currently directing users who navigate to the homepage of the old site to an interim page on our new site that reads companyname.com/companyname, that then redirects to the actual homepage of the new site, companyname.com. The redirect page's purpose is to tell users about the brand name change before showing them the new site. The challenge is that in the second step of the verification process for the address change, it reads that our domain name request does not correspond because the homepage is directing to this interim page. Is there a way for us to continue to direct people to companyname.com/companyname and have GSC verify that the 301 redirects work properly? Ideally we would not want to direct people from the homepage of the old site to the homepage of the new site. Thank you in advance for your help. Sincerely, Chase VHSpF
Technical SEO | | commcreative0 -
Should existing canonical tags be removed where a 301 redirect is the preferred option?
Hi, I'm working on a site that is currently using canonical tags to deal with www and non-www variations. My recommendation is to setup 301 redirects to deal with this issue instead. However, is it ok to leave the existing canonical tags in place alongside the new 301 redirects or should they be removed? My thoughts are that this is not a canonical issue and therefore they should be removed? If 301 redirects are not possible it would be better have them that nothing at all but I don't think we need both, right? Any feedback much appreciated!
Technical SEO | | MVIreland0 -
301 redirects don't work properly
Hello all, I've been working on 301 redirects for a bit and normally it's no problem but some seem to be going wrong. Redirect 301 /3-zits.html http://www.bankstellenshop.com/banken/3-zits.html This one works properly but the following one gives a very strange result as it goes to http://www.bankstellenshop.com/bankstellen.html/u (no idea where the .html comes from) Redirect 301 /bankstellen/u http://www.bankstellenshop.com/bankstellen/u.html Any idea what I'm doing wrong or what should change? Thanks in advance!
Technical SEO | | Kapottefietsband0 -
To 301 redirect or not to 301 redirect? duplicate content problem www.domain.com and www.domain.com/en/
Hello, If your website is getting flagged for duplicate content from your main domain www.domain.com and your multilingual english domain www.domain.com/en/ is it wise to 301 redirect the english multilingual website to the main site? Please advise. We've recently installed the joomish component to one of our joomla websites in an effort to streamline a spanish translation of the website. The translation was a success and the new spanish webpages were indexed but unfortunately one of the web developers enabled the english part of the component and some english webpages were also indexed under the multilingual english domain www.domain.com/en/ and that flagged us for duplicate content. I added a 301 redirect to redirect all visitors from the www.domain/en/ webpages to the main www.domain.com/ webpages. But is that the proper way of handling this problem? Please advise.
Technical SEO | | Chris-CA0 -
301 redirect .htaccess problem
Can anyone explain to me why this doesn't work? Redirect 301 /category/diamond-pendants/nstart/1/start/(.*) http://www.povada.com/category/pendants/nstart/1/start/$1 Im trying to replace everything after /start/ and insert it into the new url. Thanks in advance.
Technical SEO | | 13375auc30