After building a new site I have a couple of redirects in my htaccess to redirect the users to the right page for SEO reasons.
It are all 301 redirects in 1 of this format:
For pages
Redirect 301 /page.html /new-page
For product
Redirect 301 /webshop/category-name/subcategory-name/detail/123/product-name.html /product/product-name
subcategory is optional, not all products are in an subcategory, but they have at least 1 category. 123 is an product id, which is different on all products.
For category
Redirect 301 /webshop/category-name/subcategory-name /category/category-name
subcategory is optional.
That works fine, but since this website is reachable by 2 domains (dutch and english site) these redirect must only work when the domain is dutchdomain and englishdomain.
So, I read about the rewriteEngine, the rewriteCond and rewriteRule and tried this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.dutchdomain\.nl [NC]
RewriteRule /faq.html /faq/ [L,R=302]
But that isn't working. How can I get this working with the domain condition?
Is there a way to do that with an regular expresion, so that I have only a few lines instead a the 1668 lines I have now?
With the regex webshop (*.+\)\ I replaced the url's from the old site sitemap for the products. The url's for the pages and the categories's I replaced manually.
Related
I am looking to redirect and old website to a new one, but we want to do it in 2 parts;
We want to redirect the whole blog to the new blog page
http://www.websiteA.com/news
http://www.websiteA.com/news/post1
http://www.websiteA.com/news/post2 ...
Redirects to;
http://www.websiteB.com/news
We then want to redirect everything else to the homepage on the new website.
http://www.websiteA.com/page1
http://www.websiteA.com/page2
http://www.websiteA.com/page3
Redirects to;
http://www.websiteB.com/
How would I do this through the .htaccess?
You can use these 2 redirect rules in your site root .htaccess:
RewriteEngine On
# news URIs
RewriteRule ^(news)(?:/.*)?$ /$1? [L,NC,R=301]
# remaining ones
RewriteRule . /? [L,NC,R=301]
This is the current rule in the htaccess
# Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]
</IfModule>
Works fine so far but it should have one exception: if it is the home page it should redirect to a specific url. So if its http://old.com/ it should redirect to http://new.com/shop in every other case it should behave like above so http://old.com/cat/xyz redirecting to http://new.com/cat/xyz and so on.
Any suggestions?
.* in regex matches everything including landing page URI i.e. / but .+ skips landing page.
You can use these rules:
RewriteEngine On
# handle landing page
RewriteCond %{HTTP_HOST} ^(?:www\.)?old\.com$ [NC]
RewriteRule ^$ http://new.com/shop [R=301,L]
# everything but landing page
RewriteCond %{HTTP_HOST} ^(?:www\.)?old\.com$ [NC]
RewriteRule . http://new.com%{REQUEST_URI} [R=301,L,NE]
301 a single page URL to another
To create a 301 redirect from one URL to another URL, add the
following line of code:
Redirect 301 /retiredpage.html http://www.example.com/newpage.html
You can add as many of these redirect lines as necessary to the .htaccess file.
301 a directory URL and all of its contents to another
If you have redesigned your site architecture and renamed a directory, you need to create a 301 for the entire directory. Here’s how:
RedirectMatch 301 ^/oldname/ http://www.example.com/newname/
301 a domain name URL to another
If you just bought an aged domain name whose traffic (and search page rank value) you want to use to augment that of your existing site’s domain, you can set up a 301 to transfer all traffic and ranking from the purchased domain name to your current site.
Use the following code as an example:
RedirectMatch 301 ^(.*)$ http://www.example.com
Be sure you set up this redirect code in the .htaccess file of the source site you want redirected, not the redirect target site!
301 domain name URL variants for canonicalization
Since search engines index URLs, having multiple URLs in the index that point to the same content page divides the available page rank credit for that page among those URLs. This is definitely a “not optimized for search” state of affairs! To learn more about the details of canonicalization, take a look at the Search Engine Land post Why Canonicalization Matters From A Linking Perspective. The bottom line is you want to consolidate the page rank to one (canonical) URL to optimize the search value of that content.
Once you understand canonicalization best practices, you’ll want to implement them on your site. That means you must account for all redirecting possible alternative URL variations to the canonical URL. Use the following code sample for your site’s home page:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index)\.(html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.example.com/$1 [L,R=301]
The first two-line block of code redirects URLs that have omitted the “www.” prefix to the full “www.example.com” home page URL. That means the home page URL
**
http://example.com will not resolve on its own, but instead will redirect to http://www.example.com/.
**
The second code block redirects URLs specifying default page references to the URL that omits default page reference names. This code ensures that any home page URL that includes several versions of explicit page name references, such as default.htm or index.html, will be redirected to the canonical home page URL,
http://www.example.com/.
In my htaccess I want to do 301 redirects from all of the products that use the standarddetail page
https://www.domain.com/standarddetail.asp?cid=4135
To the following page:
https://www.domain.com/index.php?route=product/product&product_id=4135
I want to do this for all (cid) query strings
I have a couple of thousand products that appear on this standarddetail.asp page using the querystring (cid) and I want them to go to the new page using the product_id= querystring.
I want them to be 301 redirects.
How is this done?
In the htaccess file in your document root, add this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^cid=([0-9]+)
RewriteRule ^standarddetail\.asp$ index.php?route=product/product&product_id=%1 [L,R=301]
I need to redirect my website to a new domain name but I want it to also redirect all of the pages. I want www.myoldsite.com/ to redirect to www.mynewsite.com/ but I also want all of the pages to redirect.
For example: www.myoldsite.com/faq to redirect to www.mynewsite.com/faq and so forth and so on. I know how to do it by doing each individual page but I have a lot of pages. How do I rewrite the 301 for it to basically change the root domain name but keep the directories and pages the same? Thanks a million!
You just need this in the htaccess file in your old domain's document root:
Redirect 301 / http://www.mynewsite.com/
If both your old site and new site share the same document root, then you need mod_rewrite instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myoldsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mynewsite.com/$1 [L,R=301]
basically , google is listing all subdomain pages of my add-on domain.
google lists
http:// www. mymainwebsite.com (which is good)
but also
http:// www. subdomain . mymainwebsite.com (which is not good)
So.......... in the htaccess I added this:
RewriteCond %{HTTP_HOST} ^subdomain.mymainwebsite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.subdomain.mymainwebsite.com$
RewriteRule ^/?$ "http\://www.mymainwebsite.com/" [R=301,L]
This works for the main subdomain, BUT I can "still access" all of the subdomain pages, e.g. I can still access:
http:// www. subdomain.mymainwebsite.com/about
Google has listed 'hundreds' of these above subdomain "pages"
So my question is... is there any 'code' that i can add in my htaccess, which redirects "ALL" the subdomain "pages" which have unfortunately been listed in google?
I want http://www.subdomain.mymainwebsite.com/about (faq/history/contact/links, and hundreds more pages)
to goto
http://www.mymainwebsite.com/about (faq/history/contact/links, and hundreds more pages)
Thanks guys!
You need to tell the rewrite engine to add the rest of the URL to the rewrite.
It should look something like this:
RewriteRule ^/(.*)$ "http\://www.mymainwebsite.com/$1" [R=301,L]
That will redirect all paths on your subdomains to your main domain.
The $1 is a backreference to the (.*), which matches all characters after the slash. Using the $1 includes those characters in the redirect.