I'm really struggling to place redirects into my .htaccess file based on the cloud flare country code. I'm either getting "too many redirect errors" or 500 internal errors using examples from across this site.
I currently have this:
Options +FollowSymLinks
RewriteEngine on
SetEnvIf CF-IPCountry "(.*)$" Country=$1
RewriteCond %{ENV:Country} GB
RewriteRule ^(.*)$ http://example.com/$1 [R,L]
So I need the default domain to be http://example.com
but if a country code of GB comes from cloudflare for example, I need it to redirect to:
http://example.com/gb
With my limited knowledge of .htaccess is this possible?
Try with:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP:CF-IPCountry} ^GB$
RewriteCond %{REQUEST_URI} !(?:gif|png|jpg|jpeg|css)$ [NC]
RewriteRule !^gb/ /gb%{REQUEST_URI} [NC,NE,R,L]
Redirect http://example.com/test to http://example.com/gb/test when the request comes from a user with CF-IPCountry=GB which is not already using a link with /gb/
Related
I have a web server with several Domains pointing to it. I want one domain lets name it www.example.com to go into the folder /example in my Doc Root (e.g. /var/www/example). I do this using this .htaccess code in my Doc Root:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule ^(/)?$ example [R=301,L]
</IfModule>
Now typing in the domain in the browser gets me to the correct location but instead of seeing www.example.com or example.com in the url bar of my browser I see [IP-Address]/example (e.g. 1.2.3.4/example). Of course I want the domain name www.example.com to show up and if I surf to a specific file e.g. /example/folder/index.php I want (www.)example.com/folder/index.php to show up.
I have tried some of the proposed fixes from other stack overflow questions regarding redirect to subfolder but all of them seem to elicit the above unwanted behaviour. Any help would be greatly appreciated.
Thanks
You can use these rules:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(?!band/).*$ /band/$0 [L,NC]
Make sure to use a new browser for testing.
(?!band/) is a negative lookahead to prevent redirect when request already starts with /band/.
for our client project we need to redirect all url calls to the matching .php file extension. this is done for SEO (google) ranking. avoiding indexing both url (with *.php and without)
we try to do this using the .htaccess file (shared host) but it seemed to only work for redirects that have a different url and not to the one that just adding the ".php" extension.
different url works
abc -> abc2.php
same url doesnt work
abc -> abc.php
here is our code sample:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co.uk
RewriteRule (.*) http://www.abc.co.uk/$1 [R=301,L]
# this doesn't work
Redirect 301 /test http://www.abc.co.uk/test.php
# this redirect works as the url is different
Redirect 301 /test-abc http://www.abc.co.uk/abc.php
we host with 1&1 shared host server.
php version is 5.5
I also tried this code that redirects but the page not loading now:
RewriteRule ^(.+)\.(.*)$ http://abc.co.uk/$1.php [R,NC]
also noticed url without file extension (.*|html) will not redirect
Try this .htaccess in DocumentRoot:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co\.uk$ [NC]
RewriteRule (.*) http://www.abc.co.uk/$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L,R=302]
This appears to be a 302 re-direct, which is not SEO freindly. I tested the above and it doesnt work, also, if it did I wonder what would happen to url's that did not require the .php extension on the URL.
Basically we're trying to get a 301 re-direct working, but it seems to be conflicting with the rules set up that contain the same url. (IE abc > abc.php)
I wonder if there could be some kind of Apache setting or even and unlikely a PHP setting? the 301's work on any other linux server that I try.
Recently I have encounter a strange issue , my website www.xyz.com is being pointed by some one on the web domain let suppose www.abc.com.
Though the whole website is on www.xyz.com but the other domain display every single content and directory path structure by their domain...e.g. the real path is www.xyz.com/somepage/id/etc can be work by www.abc.com/somepage/id/etc with same directory paths....
This other website is just redirecting everything to my website and I want to stop this domain to use my directory structure. This www.abc.com is also being crawled by Google crawler and added its link in Google search engine.
This is a very new issue to me I have one solution to restrict every single request and check if its from my own website or not.
Second solution is to restrict them through htaccess but I don't find perfect solution using htaccess.
I saw on the web it stop all the referrer, but doing that I am afraid if it will stop users coming from other website to my website ...I just need to restrict other domains to use my whole website as theirs using redirection...i have taken this issue on go daddy and they said they also don't know why the other website is pointing to my ip address ... so clueless I need expert advice to secure my website from future issues like this ...kindly advice...
My htaccess is
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
and i am using YII...
You can place this rule just below RewriteEngine On line:
RewriteEngine On
RewriteCond %{REMOTE_HOST} abc\.com$ [NC,OR]
RewriteCond %{HTTP_REFERER} abc\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} !xyz\.(com|net)$ [NC]
RewriteRule ^ - [F]
In your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.xyz\.com$
RewriteCond %{HTTP_HOST} !^subdomain\.xyz\.com$
RewriteRule .* - [F]
I have to make a maintenance page for my website with the .htaccess, I've searched on the internet and could only find snippets/scripts which redirect but I only want that it displays a message on the page itself for people, but not for a specified ip-address. So when I enable the script in the .htaccess it has to show a message on every page/ file(=css, etc.) except for my ip-address
This should work.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteCond %{REQUEST_URI} !^/construction.php
RewriteRule ^ /construction.php [L]
I want to redirect http://olddomain.com to http://newdomain.com for my all urls..keeping the page on new domain same.
What i mean to say is URLs such as below
http://olddomain.com/home/category/page.html
http://olddomain.com/home/mybook/page2.html
http://olddomain.com/login
should be 301 redirect to the new newdomain but same pages, like below
http://newdomain.com/home/category/page.html
http://newdomain.com/home/mybook/page2.html
http://newdomain.com/login
this is what i have in my .htaccess currently
RewriteEngine on
RewriteCond $1 !^(index\.php|img|public|robots\.txt) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
Please help me to do this cleanly and exlpain things in details since i am new in this.
also does someone know how much time search engines might take to move away from the references of my olddomain? i mean the old-domain urls in search queries should be replaced by new-domain urls... n old domain should go away from search engines.
Add following code at the beginning of .htaccess -
RewriteEngine On
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^another.olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
Much simpler:
Redirect 301 / http://newdomain.com/
Replace your .htaccess file with that one line OR if you have access to it, put it in the apache conf file(s) for your old domain (I place it following the DocumentRoot directive).
See Redirecting and Remapping with mod_rewrite for more info.
I usually add the following codes in .htaccess in the old website
#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
This above code will redirect all links to new domain, you don't have to do anything, each link and images redirect to new domain link.
Beside this we have to tell Google when your site moves
If you've moved your site to a new domain, you can use the Change of address tool to tell Google about your new URL. We'll update our index to reflect your new URL. Changes will stay in effect for 180 days, by which time we'll have crawled and indexed the pages at your new URL.
Here is the link for this https://support.google.com/webmasters/answer/83106?hl=en
I have done this for 2 to 3 sites without losing seo as well. It does work.
Thanks
Tried to do
LoadModule rewrite_module modules/mod_rewrite.so
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
</IfModule>
But did not worked the way it should, was just trying to point my localhost to newdomain.com.
But when I hit localhost still it does not point.