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
-
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 -
301 Redirecting http to https
In the Moz Site Crawl issue, I was seeing an error that said we were temporarily redirecting our homepage to https URLs. So I changed the code in htaccess to make it 301 redirect but I'm still getting the same error. I implemented it last week and we just had a new crawl yesterday. Here is the new code: RewriteEngine on
Technical SEO | | Heydarian
RewriteCond %{HTTP_HOST} ^heritagelawmarketing.com [NC]
RewriteRule ^(.*)$ http://www.heritagelawmarketing.com/$1 [L,R=301,NC] Does anyone know why I'm still getting 302 redirects? Thanks0 -
Will doing a 301 redirect for one domain to another give the latter domain the formers links?
I have some websites that I built a few years ago that are still in existence, but I no longer have access to the sites as they weren't hosted by myself. These sites all carry a "Designed by Me" text on the footer with a link to my (now old) website. I have since done 301 redirects on the domain names that are used in the footers of these sites so they link directly to my new site. However, will these websites now show up on Google Webmasters for example as external links to my site?
Technical SEO | | mickburkesnr0 -
Redirect a 301 Redirect
Does any link juice get passed from a permanent redirect to a new 301 redirect? If so, are there any studies which indicate an estimated percentage?
Technical SEO | | RedCaffeine0 -
Htaccess redirects how many can i have and does it slow site down
I have had to redesign my site this year www.in2town.co.uk because my hosting company made a huge mistake while trying to update the joomla on my site which resulted in me losing thousands of pages and links. What i would like to know is, i have put some of the old urls in my htaccess file but i would like to know how many old urls i can have in my htaccess file as i am unsure how to use it properly. my idea was, to have some of the lost url links to my site and put them in my htaccess file and have them pointing to similar pages. not sure if this is a good idea or not. i think i have lost a few hundred good links but i would like to know if these urls in the htaccess file would slow down my joomla site any advice would be great.
Technical SEO | | ClaireH-1848860 -
301 redirects
At the moment it's possible to access the home page of my website via two different urls, with and without www. and you've told me that this can be resolved with Canonicalization and a 301 redirect. Do I do this with my web hosting package or in my html pages? If I can't do it with my web host (1&1) then is there an idiot's guide of how to do it yourself? I've also got both the domain vamospaella.co.uk and vamospaella.com. Is it better to have one of these redirecting to the other for UK traffic (at the moment .co.uk redirects to .com) Thanks
Technical SEO | | melissa10 -
Isapi rewrite syntax for partial link redirects
I need to redirect a number of partial (incomplete) redirects. The listing in Google Webmaster Tools looks like this: learn/how_to_dr learn/howtodrea/a learn/ingredie.. learn/step_by_step_instruc When I try and use Isapi to redirect these, It does not work. Is there a specific syntax I need to do to accomplish this?
Technical SEO | | dreadmichael0 -
Not sure which URL to use for 301 redirect
A client has new website design completed by another developer, was launched in April of this year. No 301 redirect was set up so duplicate content is an issue. Client has had a website with same domain name for about 10 years, but has not had any SEO work completed before or since his new site design. For non-www there are 6 referring links - 1 considered to have authority, for www there are also 6 but 3 considered to have authority. More links seem to coming from www than non-www. But for one of the clients keywords they are ranked #1 for their area and that links to their non-www address. And even though no redirects set up by developer, non-www has had far more visits according to Google Analytics. So many basics that still need to be done for site: no meta-descriptions on any page, H1 and page titles could use keywords, call to action moved above fold, etc. Considering this is a new site, and new SEO work and many more inbound links needed, does it matter which address I redirect to? _Cindy Barnard
Technical SEO | | CeCeBar0