.htaccess two domains for languages ignore folder - .htaccess

Given this directory structure:
.
├── assets
│ └── img/
│ └── foo.png
├── en
│ └── index.php
└── fr
└── index.php
There are two domains:
english.com
french.com
My goal is to redirect
english.com => english.com/en
french.com => french.com/fr
english.com/fr => french.com/fr
french.com/en => english.com/en
no redirects for:
english.com/assets/img/foo.png
french.com/assets/img/foo.png
The current solution is not working propery how to achieve the goals above
<IfModule mod_rewrite.c>
RewriteEngine On
# no www, https only
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} ^www\.%{HTTP_HOST}$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(en) - [L]
RewriteRule ^(fr) - [L]
RewriteRule ^(assets) - [L]
redirectMatch 301 ^/en$ https://english.com/en
redirectMatch 301 ^/fr$ https://french.com/fr
RewriteCond %{HTTP_HOST}: englisch.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/en/$1 [R=301,L]
RewriteCond %{HTTP_HOST} french.com$ [NC]
RewriteRule ^(.*)$ https://french.com/fr/$1 [R=301,L]
</IfModule>

Have it like this:
RewriteEngine On
# no www, https only
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
# ignore all rules for anything in assets/ directory
RewriteRule ^assets/ - [L,NC]
# for fr/ host name should be french.com
RewriteCond %{HTTP_HOST} english\.com$ [NC]
RewriteRule ^fr/ https://french.com%{REQUEST_URI} [R=302,L,NE]
# for en/ host name should be english.com
RewriteCond %{HTTP_HOST} french\.com$ [NC]
RewriteRule ^en/ https://eglish.com%{REQUEST_URI} [R=302,L,NE]
# for english.com URI must start with en/
RewriteCond %{HTTP_HOST} english\.com$ [NC]
RewriteRule !^en/ /en%{REQUEST_URI} [R=302,L,NE]
# for french.com URI must start with fr/
RewriteCond %{HTTP_HOST} french\.com$ [NC]
RewriteRule !^fr/ /fr%{REQUEST_URI} [R=302,L,NE]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Related

Force redirect permanently on RedirectMatch from $_GET url to semantic url

I am doing some .htaccess settings to improve SEO with semantic URL's. Currently when you go to the semantic URL www.website.com/profile/exampleusername it works perfectly although i am still able to access the same page without any red alarms rining with the pure get request in the url. www.website.com/profile?username=exampleusername.
I have tried to user both the redirect and RedirectMatch but i can't get it to work.
Here is the relevant code from the .htaccess
RewriteCond %{THE_REQUEST} /profile\.php\?username=([^\s&]+)&buddy_identifier=([^\s&]+) [NC]
RewriteRule ^profile/%1/%2? [R=302,L]
RewriteRule ^profile/([^/]+)/([^/]+)/?$ profile.php?username=$1&buddy_identifier=$2 [L,NC,QSA]
I want to be able to go to www.website.com/profile?username=exampleusername and then be redirected to www.website.com/profile/exampleusername
Full .htacces
RewriteEngine on
Options -MultiViews +FollowSymLinks -Indexes
RewriteCond %{ENV:REDIRECT_STATUS} ^$
############# REMOVE FILE EXTENSION #############
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
#####
######## ONLY ACCESS BY HTTPS://WWW.* AND NO EXTENSION #############
#commented bcs of dev workspace localhost
#RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^/]+)\.php(\?|\ |$)
#RewriteRule ^ /%2 [L,R=301]
#RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
#RewriteCond %{HTTPS} off [OR]
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
#RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
############# INDEX RENAME #############
DirectoryIndex gamingbuddy.php
################### SEMANTIC URL ######################
### URL FOR GAMES.PHP ###
RewriteCond %{THE_REQUEST} /games\.php\?game=([^\s&]+)&game_style=([^\s&]+) [NC]
RewriteRule ^games/%1/%2? [R=302,L]
RewriteRule ^games/([^/]+)/([^/]+)/?$ games.php?game=$1&game_style=$2 [L,NC,QSA]
### URL FOR PRIVATE PROFILE ###
RewriteCond %{THE_REQUEST} /profile\.php\?username=([^\s&]+)&buddy_identifier=([^\s&]+)\s [NC]
RewriteRule ^profile/%1/%2? [R=302,L]
RewriteRule ^profile/([^/]+)/([^/]+)/?$ profile.php?username=$1&buddy_identifier=$2 [L,NC,QSA]
You can use these rules:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} /profile(?:\.php)?\?username=([^\s&]+)\s [NC]
RewriteRule ^ /profile/%1? [R=302,L]
RewriteRule ^profile/([^/]+)/?$ profile.php?username=$1 [L,NC,QSA]

Redirect all domains to use HTTPS

I have this code in my .htaccess file which does as the comments say:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
Options -Indexes
# redirect all traffic to correct domain
RewriteCond %{HTTP_HOST} ^itl\.|^(www\.)?(integratelecom|integra|integratelecommunications)\b [NC]
RewriteRule ^ https://www.example.net%{REQUEST_URI} [L,R=301,NE]
# redirect admin./ssladmin. sub domain to the correct folder
RewriteCond %{HTTP_HOST} (admin|ssladmin)\.itl\.uk\.net$ [NC]
RewriteRule !^admin/system/ admin/system%{REQUEST_URI} [L,NC]
# redirect subdomain.X to subdomain.example.net
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(?!itl\.)[^.]+\. [NC]
RewriteRule ^ https://%1.example.net%{REQUEST_URI} [L,NE,R=302]
# map subdomains to correct folder
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.net$ [NC]
RewriteRule !^subdomains/ subdomains/%1%{REQUEST_URI} [L,NC]
RewriteRule ^(subdomains/|admin/|index\.php$) - [L,NC]
# stop external linking to website but allow listed domains below
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.co.uk [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(.*\.)?itl.uk.net [NC]
#RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.|admin\.|ssladmin\.)?example.net [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
#######################################
############## MAIN SITE ##############
#######################################
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(blog)/(post|tags|category)/([\w-]+)/?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC,L]
RewriteRule ^(support/knowledgebase)/(article|category|search)/([\w-]+)?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
All of the above is working fine, but i need to force every domain and subdomain to use HTTPS, I have tried adding in:
RewriteCond %{HTTPS} off %HTTP_HOST [NC]
RewriteRule ^ https://%1.domain.net%{REQUEST_URI} [L,NE,R=302]
But that keeps returning an Internal Server Error
I also tried these two lines, but that adds %25 to the end of the URL
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]
To force HTTPs you can use:
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
It basically says, if HTTPs is not equal to ON, then it will force the website to display using SSL.
Make sure you clear your cache before testing this.

.htaccess redirect non-www and non-http to https://www

I'm trying to redirect:
http://example.extension
https://example.extension
http://www.example.extension
to
https://www.example.extension
using:
RewriteCond %{HTTP_HOST} ^example.extension$ [OR]
RewriteCond %{HTTPS_HOST} ^example.extension$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.extension$ [NC]
RewriteRule (.*) https://www.example.extension$1 [R=301,L]
http://example.extension is redirected to https://www.example.extension, however, I receive the error:
The page isn’t redirecting properly
From this answer, if I change my .htaccess rules to:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.extension%{REQUEST_URI} [R=301,L,NE]
the same symptom occurs, even after clearing the browser cache.
Also, I have some sub-domains I don't want to redirect to www, like:
https://my.example.extension
https://forum.example.extension
I need http://*.example.extension redirected to https://*.example.extension
Other than the rewrite rules I am requesting help with, the only other content in .htaccess is Wordpress related:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# END WordPress
# BEGIN MainWP
# END MainWP
Help appreciated.
There is no variable called HTTPS_HOST.
Replace your rule with this rule:
RewriteEngine On
# main domain: add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.extension)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# sub domain
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Make sure to clear your browser cache completely.
With CloudFlare, You must use CloudFlare Page Rules:
https://support.cloudflare.com/hc/en-us/articles/200170536-How-do-I-redirect-all-visitors-to-HTTPS-SSL-
But you can use:
RewriteEngine On
#main domain
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.extension)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#sub-domains
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

.htaccess file showing 500 Internal Server Error

I have this in my htaccess file:
RewriteCond %{HTTP_HOST} ^my\.domain\-uk\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.my\.domain\-uk\.net$
RewriteRule ^/?$ "http\:\/\/www\.domain\-uk\.net\/my\-integra" [R=301,L]
RewriteCond %{HTTP_HOST} ^status\.domain\-uk\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.status\.domain\-uk\.net$
RewriteRule ^/?$ "http\:\/\/www\.domain\-uk\.net\/service\-status" [R=301,L]
RewriteCond %{HTTP_HOST} ^dd\.domain\-uk\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.dd\.domain\-uk\.net$
RewriteRule ^/?$ "https\:\/\/sub\.domain\.co\.uk\/preauth\/0J9A7MT35N" [R=301,L]
the status. redirects but the two after are showing 500 Internal Server Error
above this i have the standard Wordpress code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The first rules that you want in your htaccess file are the ones the redirect the browser, the order is important, you don't want to redirect the browser after wordpress's internal rewriting. So first, you can clean up your redirect rules:
RewriteCond %{HTTP_HOST} ^(www\.)?my\.domain\-uk\.net$ [NC]
RewriteRule ^/?$ http://www.domain-uk.net/my-integra [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?status\.domain\-uk\.net$ [NC]
RewriteRule ^/?$ http://www.domain-uk.net/service-status [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?dd\.domain\-uk\.net$ [NC]
RewriteRule ^/?$ https://sub.domain.co.uk/preauth/0J9A7MT35N [R=301,L]
The ? makes the grouping optional, and the NC ignored the case. So after these rules, you can put your wordpress rules.

restrict https access with .htaccess to everything but 1 folder

I would like to have the folder https_folder (including all subfolders and files) forcing https, but every other directory or file, utilizing http.
Folder structure:
foldera
folderb
https_folder
folderc
I tried to set it as followed, however i dont seem to get it working
redirect for http /https_folder
RewriteCond %{SERVER_PORT} = 80
RewriteRule ^https_folder/?$ https://%{HTTP_HOST}%/httpd_folder [R=301,QSA,L,NE]
redirect for https non /market pages
RewriteCond %{SERVER_PORT} =443
RewriteCond %{REQUEST_URI} !^/https_folder [NC]
RewriteRule ^/?(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
Any help would be appreciated
Do this:
RewriteCond %{REQUEST_URI} /https_folder/? [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(https_folder)(/.*)?$ https://%{HTTP_HOST}/$1$2 [R=301,L,NE]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/https_folder/? [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L,NE]
Put this code in your .htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# redirect to https if URI has https_folder
RewriteCond %{HTTPS} off
RewriteRule ^https_folder/? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
# redirect to https if URI doesn't have https_folder
RewriteCond %{HTTPS} on
RewriteRule ^(?!https_folder/?)(.*)$ http://%{HTTP_HOST}/$1 [R=301,L,NC]

Resources