GDY
GDY

Reputation: 2941

Htaccess rewrite rule with exception for home page

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?

Upvotes: 2

Views: 1186

Answers (2)

anubhava
anubhava

Reputation: 785186

.* 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]

Upvotes: 2

norcal johnny
norcal johnny

Reputation: 2115

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/.

Upvotes: 0

Related Questions