htaccess rewrite URL if domain doesn't equal and subfolder is this - php

I'm trying to get a rewrite working and need a hand...
I've got a set of websites, which domains are like this
example.com
example.co.uk
example.ie
example.com/gm
The way the site is setup means that all domains can display the .com/gm content (GM doesn't have it's own domain and we want it only served from the .com domain)
So I'm trying to redirect anyone using incorrect URLs to the correct TLD with the gm subfolder. E.G:
going to www.example.co.uk/gm would 301 to www.example.com/gm
I've currently got this:
RewriteCond %!{HTTP_HOST}:{REQUEST_URI} !^example.com:gm$ [L,R]
RewriteRule ^(.*)$ https://www.example.com/gm$1 [R=301,L]
Which obviously isn't happy
Appreciate a point in the right direction, basically from what I can tell I need a rule that checks the domain doesn't equal .com if they're trying to get to /gm/ and redirects them to .com/gm/
Thanks!!

It is easier to check if the /gm/ path was requested in the RewriteRule, and prefix that with a RewriteCond that checks for the host name – something like this should do it:
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(gm/.*)$ https://www.example.com/$1 [R=301,L]

Your htaccess is full of syntax errors.
Try the following:
RewriteCond %{HTTP_HOST} ^(www\.)?example.co.uk$
RewriteCond %{REQUEST_URI} ^/gm/
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
This will redirect
example.co.uk/gm
to
example.com/gm
Clear your browser's cache before testing this.

Related

htaccess rules to rewrite subfolder to a GET variable

I'm not very experienced with htaccess. today I fail in writing an .htaccess to rewrite a url.
What I want:
My project is hosted on a subdomain e.g. sub.domain.com
My htaccess should manage:
sub.domain.com should redirect to www.domain.com (Redirect 301, that's clear)
subfolders should be put as GET variable e.g. sub.domain.com/test to www.domain.com?var=test
the subfolder /admin shouldn't be redirected
Thanks in advance for tips!
This should do it for you:
RewriteEngine on
RewriteCond %{HTTP_HOST} =sub.example.com
# Following condition added to support changed requirement, see comments
RewriteCond %{QUERY_STRING} !(?:^|&)var=
RewriteCond %{REQUEST_URI} !^/admin/
RewriteRule ^(.*)$ http://www.example.com/?var=$1 [R=301,L]
This will drop any query string that was in the original request. If you want to keep any query string, and just append 'var=...' to it, then add QSA to the flags at the end of the last line, separated by a comma.

PHP - How to avoid infinite loop in Redirect by htaccess

I want to redirect my visitors to a specific virtual url according the country they are. For example, if you are in Usa and enter www.mysite.com/subsite, you are redirect to www.mysite.com/subsite/US/
If you enter www.mysite.com/subsite/contact.php you have to be redirect to www.mysite.com/subsite/US/contact.php. Same for another country, for ex, Argentina (AR), you will be redirect to www.mysite.com/subsite/AR/contact.php . In the back, you will always see the physical content of www.mysite.com/subsite/contact.php.
I was reading a lot about this and try to do it by editing htaccess, and it works (sort of...) but I got an infinite loop. I try to make a rule that says "if you don't have the string 'AR' in your url, make the redirect, otherwise, stay as you come", but it isn't working. Could anyone please help me. Here's my code so far:
GeoIPEnable On
RewriteEngine On
RewriteOptions MaxRedirects=1
RewriteCond %{HTTP_HOST} ^(?!.*AR).*$ [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AR$
RewriteRule ^(.*)$ /subfolder/AR/$1 [R]
Your problem is this line:
RewriteCond %{HTTP_HOST} ^(?!.*AR).*$ [NC]
The variable %{HTTP_HOST} is the domain name requested by the browser. To check the path, you would use %{REQUEST_URI}:
RewriteCond %{REQUEST_URI} ^(?!.*/AR(?:$|/)) [NC]

Use subdomain to make get parameter for php

I found couple of same questions but their answers not working for me. They dont make folders acceptable. Here is my question:
I want to translate:
test.localhost/folder/&anothervar=1
to
localhost/folder/index.php?subdomain=test&anothervar=1
I will use it on some domain too I'm developing the site right now so I need to figure out how to make this work before another parts.
Thanks,
You will have to go into your .htaccess file to solve this.
Once there, enter the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.com [NC]
RewriteRule (.*) http://www.%2.com/?test=%1 [R=301,L]
This will essentially convert subdomain.domain.com into domain.com/?test=subdomain
You can take it from there and add additional parameters.
For other subdomain related stuff, check this out.
Hope that helps!
To translate
test.localhost/folder/&anothervar=1
to
localhost/folder/index.php?subdomain=test&anothervar=1
You can use the following rule :
RewriteEngine on
#1)if host value is =="test.example.com"
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.([^.]+) [NC]
#2)And the uri is =="folder/&anychar"
#Redirect the request to "http://%1/folder/index.php?test=sub&anychar
RewriteRule ^([^/]+)/&(.+)$ /$1/index.php?test=%1&$2 [B,R=301,L]

How to use .htaccess to redirect example.com.tw to example.com?

Summary
I have bought two domains, both 'example.com.tw' and 'example.com'. I hope that when a user input 'example.com.tw' in the address bar, it can redirect to 'example.com' automatically. Below is my try.
My try
RewriteCond %{HTTP_HOST} ^www\.example\.com\.tw [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]
Questions
Is it correct above?
Is there any better way to improve the code above?
The issue is that when a user input the subdomain (Such as: 'news.example.com.tw'), will it redirect correctly? (Will it redirect to 'news.example.com' successfully?)
To remove the .tw TLD:
Assuming both domains point to the same place and any subdomains are simply in subdirectories off the main domains document root (or point to the document root of the main domain itself). Then, in your root .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.tw$
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
Replace 302 (temporary) with 301 (permanent) when you are sure it's working OK.

How to use a rewrite rule to redirect to https but relative?

Is there a way to redirect to a specific page but adding https and keeping the path relative at the same time?
RewriteEngine On
RewriteRule ^user/([^/]+)/settings/?$ https://%{HTTP_HOST}/settings/settings.php?u=$1 [QSA,L]
The above works well but it's somehow absolute as we start with https://%{HTTP_HOST}
And this makes the end URL to show the exact request: https://www.mydomain.com/settings/settings.php?u=John
Can we, instead, redirect to a page, adding https in a relative way? so that the exact path keeps being hidden and we only see a "folder structure": https://www.mydomain.com/user/John/settings
Thanks for your help!
Trick is to do http->https (R=301) rule on pretty URL before rewriting it internally. So something like this would work:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteRule ^user/([^/]+)/settings/?$ /settings/settings.php?u=$1 [QSA,L,NC]

Categories