Exclude a substring in htaccess RewriteRule redirect - .htaccess

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.

Related

Changing my website from dynamic CMS to static HTML. Should I 301 redirect or use other methods?

I am changing my website from a dynamic CMS-system (Umbraco) to a static classic .HTML. It is the on the same domain, but the URL will change.
Example: The URL is changing from:
www.example.com/information
To:
www.example.com/info.html
My question is:
What is the best way to redirect while keeping the best SEO page rank.
I am thinking about 301 redirect through .htaccess, but I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
Or maybe there is a different better way?
I do have a fine 404.
Also I need the right redirect code for .htaccess - if that's the right way.
I hope you guys can help me out.
I haven't try anything out yet, because I don't wanna do 301 before the site go live.
You need to implement 301 redirects from the old URL to the new URL in order to preserve SEO and ensure that any "old" links that have been bookmarked or linked to from other websites still work.
Exactly how you implement the 301 redirect (either in your server-side script or in .htaccess) does not really matter. However, if you are moving to an entirely static site then .htaccess is likely the only option you have.
I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
You need to redirect from the "old" URLs to the "new" URLs that you are using/linking to on the new site. (It makes no sense to redirect the other way as that would just break everything!)
You can probably just use the simple mod_alias Redirect directive.
For example, to 301 redirect from /information to /info.html you could do the following:
Redirect 301 /information /info.html
Bear in mind that 301 redirects are cached persistently by the browser. To prevent caching issues it is advisable to test first with a 302 (temporary) redirect.
Have you considered keeping the same URLs? This would obviously negate the need for implementing redirects. You could employ URL-rewriting if the underlying file is called info.html. For example, using mod_rewrite:
RewriteEngine On
RewriteRule ^information$ info.html [L]
The above would internally rewrite a request for /information to info.html. The user only sees /information in the browser address bar, but info.html is served from your site.
Taking this further, it would be easier if the new "file" is simply the same as the old URL, just with a .html extension. For example, the URL is /information and the underlying file is information.html. You can then use a single rule to rewrite all your URLs. For example:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
The above assumes the old URLs do not contain additional slashes (ie. consist of a single path segment. In other words, all files are in the document root) and do not contain dots.
White
I finally got my page ready to go live, and i changed all my new URLS to the same name as the old URL, just with a .html extension - as u said. After that i used:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
And it works fine.
I do have a question about if a "RewriteRule ^(.*).html$ /$1 [L,R=301]" would be better? I mean both "/page.html" and "/page/" works, and this could mess with my former SEO ranking?
Also: what do u think of this:
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
And by the way. Do you use "page.html" or just "/page" in your href to prevent the .html showing?
//MM

Redirect ALL pages from an old Domain to a new page on a different domain

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/

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

I want a user who made request from example.com to mydomain.example/domains to redirect to mydomain.com/userdomain

I want a user who made request from example.com to mydomain.example/domains to redirect to mydomain.example/userdomain. It is like implementing vanity URLs as per the user's domain name.
I know, we can do it using .htaccess redirect rules but don't know how to do.
I've EDITED the following code as best I can without a way to test it. It should get you close if it doesn't work.
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^/(.*) http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^/(.*) mydomain.example/%1/$1 [L]
should do the trick. Can't test it quickly myself though. There are two key things to notice:
You gotta use HTTP_HOST in a RewriteCond to test the domain. Use %1 in the RewriteRule line to get the variable from the HTTP_HOST line in parentheses.
You also gotta redirect any requests to www to the non www with an R=301 permanent redirect first. It might be possible NOT to do this, but I'm not sure at the moment how to extract the non www part of the host regardless of its presence. The R=301 sends a redirect msg back to the browser for anyone who visits www.example.com. Then the browser will hit example.com and your second RewriteRule will match against it. Since its 301 (permanent) the browser will cache the redirect and always do the non-www for the user on its own.
If this doesn't work, compare to some other tutorials on URL redirection and also refer to this wonderful page:
http://httpd.apache.org/docs/2.2/rewrite/intro.html

Mod Rewrite redirect URL with query string to pretty url

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]

Resources