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/.
Related
I am trying to use htaccess to redirect ALL pages from a domain to a specific page on a new domain. Yes, I completely understand we will loose SEO value this way.
I currently have a cpanel redirect that makes this url:
https://www.kiss1047.net/
go to this
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/
but that doesn't get any of the internal pages to redirect. I would also like all internal pages (here is an example):
https://www.kiss1047.net/listen-live
to also go to:
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/
I have tried a few things, but they always carry over the page url, ie above /listen-live/
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/listen-live/
and that results in a 404.
is there some htaccess magic i can employ here?
In your .htaccess file .. Make a single entry that looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://mytown-media.com/stations/kiss-104-7-kxnc-fm/ [R=301,L]
This will direct ALL TRAFFIC (.*) to your other website .. Regardless of file name or directory .. And will not append the file/directory to the end of the URL .. IE listen-live
This is a 301 Permanent redirect [R=301,L] .. Which means once followed by Google, will be indexed as such .. Also will cache in users browsers so that the browser remembers the 301 instead of bouncing between websites as well.
This command in .htaccess redirects every page of your old domain to new domain's one specific URL.
RewriteEngine on
RewriteRule ^(.*)$ http://www.newdomain.com/ [R=301,L]
For your case:
RewriteEngine on
RewriteRule ^(.*)$ https://mytown-media.com/stations/kiss-104-7-kxnc-fm/ [R=301,L]
In result:
https://www.kiss1047.net/listen-live
will be redirected to:
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/
I'm trying to set up a .htaccess redirect based on a certain url param, which then redirects to another site.
Example:
Original URL: www.domain.com/?param=true
Redirected URL: www.newdomain.com
I've tried many different approaches, but nothing seems to be working.
This rule should work from site root .htaccess of www.domain.com:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?param=true[&\s]
RewriteRule ^ http://newdomain.com/? [L,R=301]
I manage an old site with multiple domain with the same name,
google has indexed some old url with the language structure
http://www.example.com/eng/whatever
and
http://www.example.it/eng/whatever
and now I don't use this structure anymore, but I want to do a redirect to my new structure
http://www.example.it/en/whatever
so everything after the /eng/ must be appended to the new structure, excluded the /eng/ substring.
But it doesn't work, I have a redirect to
http://www.example.it/en/eng/whatever
which is wrong! I did not want /eng in the redirect url
In my .htaccess I have this
#this will redirect the url with /eng/whatever to www.example.it/en/whatever, regardless from the top level domain
RewriteRule ^eng\/(.*)$ http://www.example.it/en/$1 [R=301,L]
#redirect example.com on www.example.it/en/
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*example\.(com|pt)?(:[0-9]*)?$ [NC]
RewriteRule ^(.*)$ http://www.example.it/en/$1 [R=301,L]
Try replacing all of that with just this:
RewriteRule ^eng/(.*) en/$1 [R,L,NE,DPI]
If it works, you can change the R to R=301
Warning
To test this, you must use a new browser. This is because browsers cache 301 redirects, so your current browser will keep redirecting to the old (wrong page) without even talking to your server and hitting the new .htaccess—unless you clear the browser cache.
My site passes a variable (a phone number) in the URL between pages. It grabs the phone number from the database and writes the URL link as: url.com/phone-number/id.
However, if the target page does not have a phone number it is replaced with a 0, so,
url.com/0/id.
In my .htaccess file to rewrite old query php parameters to clean urls I put a default url.com/0/id in the rewrite. Old pages did not have a phone number so all redirect will have the 0.
# 301 redirects ad
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteCond %{QUERY_STRING} ^id=(\d+)$ [NC]
RewriteRule ^page.php$ /page/0/%1? [R=301,NE,NC,L]
My problem is Google is indexing all new pages with the url.com/0/id and not url.com/phone-number/id, even though if you browse the site you will see url.com/phone-number/id for pages with phone numbers.
I am not sure if it is my URL rewrite or Google bot's behavior that is causing this.
Don't use R=301 for above rule as 301 means permanent redirect. Use 302 (temporary redirect):
RewriteCond %{QUERY_STRING} ^id=(\d+)$ [NC]
RewriteRule ^page\.php$ /page/0/%1? [R=302,NC,L]
I successfully changed my URLs from ugly ones with several parameters in the querystring to clean looking ones with the help of mod rewrite. However, there are many url's for my site. Rather than go back and edit the href attribute on each and every one of my anchor tags, I tried to write a redirect function in the .htaccess file that automatically redirects the old url to the new one.
In my .htaccess file, I have the following:
RewriteEngine On
Redirect teams.php?league=$1&team=$2&year=$3&tab=$4 teams/(.*)/(.*)/(.*)/(.*)
RewriteCond %{REQUEST_URI} !^(css|js|img)/
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L]
No luck though... any thoughts?
Thanks
You need to do a check that the old URL with the php in it is actually being requested by matching against %{THE_REQUEST}, otherwise it'll redirect loop forever (e.g. user goes to team.php, serve redirects to teams, browser requests teams, server rewrites as teams.php, server sees "teams.php" and redirects to teams, browser requests teams, server rewrites as teams.php, etc. etc.)
RewriteEngine On
# redirect when the user actually requests for teams.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /teams\.php\?league=([^&]+)&team=([^&]+)&year=([^&]+)&tab=([^\ ]+)
RewriteRule ^teams\.php$ /teams/%1/%2/%3/%4? [R=301,L]
# internally rewrite any /teams/ URI
RewriteCond %{REQUEST_URI} !^(css|js|img)/
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L]