Non-www RewriteRule not working on request URI - apache

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>

Related

mod_rewrite rule for "www to non www" doesn't redirect sub-pages

I have made a thorough search before I have asked this question here. Please hear me out:
I am trying to redirect my blog from www to non www and it doesn't redirect any sub-pages. I have an http > https redirect in place as well and it works perfectly for both domain as well as the sub-pages. Here are the rules I have in my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^^rcp-pep-ipn //?rcp-pep-listener=IPN [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
# END WordPress
I'd really appreciate an explanation if I am doing anything wrong here. I have literally pulled my hair since I have used the exact same code (from the second RewriteBase /) for all other sites and it worked flawlessly.
You should bring those protocol checking conditions to the beginning. You have some problems within the rules too. Try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,NE,L]
RewriteRule ^rcp-pep-ipn /?rcp-pep-listener=IPN [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

ERR_TOO_MANY_REDIRECTS after updating .htaccess rules

I'm getting an ERR_TOO_MANY_REDIRECTS error on my website. I have set up a .htaccess file on my Apache server containing the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) index.html [L]
RewriteRule ^index\.html$ - [L]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
</IfModule>
This is cobbled together from two sources: Ember.js' routing tutorial and a guide to forcing HTTPS.
There must be some kind of redirect loop somewhere here, but I am unable to determine where it's coming from.
Update: I was able to get it working using the following.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html [L]
RewriteRule ^index\.html$ - [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

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

Multiple Re-Write Conditions

Is there a way I should be writing multiple re-write conditions for my domain? I want the I.P to forward to the domain name and all domain name variations forwarded to https. The domain can be found at https://www.getrideshare.com. I've included the snippet below in the .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^107\.180\.51\.9
RewriteRule (.*) https://getrideshare.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You can do your rule this way.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^107\.180\.51\.9 [OR]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://getrideshare.com/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Let me know how it works out.

Apache htaccess - route all requests to index.php except those to certain directories

Currently, my .htaccess file is routing all requests where the file does not exist to index.php. I would like to alter this so that it routes all requests to index.php regardless of whether or not the file exists, except for certain directories/paths. (such as /js, /css, /img)
Here is my current configuration:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'm having trouble figuring out how to do this with the Google or apache's documentation. Help is much appreciated, as are any general tips on this issue.
The following should work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} ! ^/css/ [OR]
RewriteCond %{REQUEST_FILENAME} ! ^/img/ [OR]
RewriteCond %{REQUEST_FILENAME} ! ^/js/
RewriteRule . /index.php [L]
</IfModule>