Use .htaccess to rewrite address from xxx.example.com to example.com/xxx/ - apache

I am looking for a way so that users can access their page by typing xxx.example.com and are then forwarded to their respective WordPress post.
Now I already have two rewrite functions on my site: (not written by me)
To use https everywhere:
#Rewrite everything to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
And the WordPress:
# 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
Now, that WordPress function seems to translate example.com/post_title to the corresponding post.
But how do I rewrite it so it translates post_title.example.com to the corresponding post?
And if possible, still showing the xxx.example.com instead of example.com/xxx

The following should work, change your .htaccess to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Rewrite to https
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
# This rewrite rule will only be used if
# we have an empty query string and the http host does not start with www
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^$ http://%{HTTP_HOST}/%1 [L]
# The default wordpress rewrite rule
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
For this to work I guess you need to setup a correct permalink structure in your wordpress installation:

Related

htaccess rewrite https from www to non www

I have seen other answers, but none accepted and there seems to be different answers that some say work and some say don't.
The below works for almost all the re directions I need for https://domain.com . Non-SSL www to non www, force SSL etc.
However, what I still can't get work is something for the https www to non-www redirection.
Any help would be appreciated.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You can use:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Non-www RewriteRule not working on request URI

I need a website to have all URLs NOT have www in front of it. For this, I have found some useful bits of code here that have helped me out a lot. However, nowhere can I find how to properly do it for ALL request URIs. Currently it only works on the domains homepage.
# 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]
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^(.*)$ https://domain.nl%{REQUEST_URI} [L,R=301]
</IfModule>
# END WordPress
Right now www.domain.nl gets rewritten into domain.nl. However, any other like www.domain.nl/anything_else do not get rewritten (also tried adding a / before the %{REQUEST_URI}, did not work).
Any help is appreciated!
With www rule before:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^ https://domain.nl%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Remove www as well as add https to a domain name

I'm looking to do the following rewrites:
http://website.com > https://website.com
www.website.com > https://website.com
https://www.website.com > https://website.com
Basically remove www of every combo and redirect everything to https.
Below is the current .htaccess config which seems to solve the first and second one above kind of well but falls apart on the 3rd one. Unable to see what's missing here.
<IfModule mod_rewrite.c>
RewriteEngine On
# remove www if it has it
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://website.com/$1 [L,R,QSA]
# send everything to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>
Actually this can be done in a single rule itself:
RewriteEngine On
RewriteBase /
# remove www if it has it
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^ https://website.com%{REQUEST_URI} [L,R=302,NE]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

.htaccess ensure www redirect incorrect

I am trying to ensure that urls always have www in front of them for canonicalization reasons.
Unfortunately when I put the following url in:
http://website.net/handbags/1/12/this-is-some-text
It redirects me here:
http://www.website.net/?controller=handbags&path=1/12/this-is-some-text
I would like to add it works fine when using:
http://www.website.net/handbags/1/12/this-is-some-text
I want it to redirect me here...
http://www.website.net/handbags/1/12/this-is-some-text
I'm not sure what could be causing this error. Here is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# CORE REDIRECT
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{7})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
You can do what I've done above; however, it's just going to redirect to http://www.website.net/handbags/1/12/this-is-some-text and then it's immediately going to hit the "CORE REDIRECT" rule and send you to http://www.website.net/?controller=handbags&path=1/12/this-is-some-text.
Is there a reason you don't want it to redirect to "?controller=handbags" when they go to www?
UPDATE
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
Rewritecond %{HTTP_HOST} !^www\.website\.net
RewriteRule ^(.*)$ http://www.website.net/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
Whenever your # CORE REDIRECT matches, the other rules are not even checked, as the core redirect rule has the Lflag set. That ones tells Apache to stop trying other rules. Remove that flag or change the rule. ;)

How to redirect specific links to https:// everything else to http:// using .htaccess?

I am trying to use htaccess to automatically direct all requests to certain pages to https and everything else to http://
Here is the code I've got the code to force ssl working below
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-homebuyers-guide https://domain.com/va-homebuyers-guide/ [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-purchase-request https://domain.com/va-purchase-request/ [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-streamline-refinance https://domain.com/va-streamline-refinance/ [R=301,L]
This is working as expected and redirects to the ssl versions of those pages. I then tried adding the following to redirect everything else to http:
RewriteCond %{SERVER_PORT} !^80
RewriteCond %{REQUEST_URI} !^va-homebuyers-guide$
RewriteCond %{REQUEST_URI} !^va-purchase-request$
RewriteCond %{REQUEST_URI} !^va-streamline-refinance$
RewriteRule ^(.*)$ http://domain.com/$1 [R,L]
This code results in a redirect loop when going to /va-purchase-request /va-homebuyers-guide and /va-streamline-refinance and does not redirect the other pages at all.
I'm totally stuck with this and any help would be massively appreciated!
Edit:
I also have this code in the .htaccess file added by wordpress... Could it be interfering with the other redirects?
# 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
Replace your .htaccess code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
RewriteCond %{HTTPS} on
RewriteRule (?!^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$)^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]