.htaccess remove www and index.php and force to https - apache

i have this code for
1-remove www from domain
2-remove index.php from end of url
3-force user to ssl
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
ErrorDocument 404 https://example.com/404.php
every thing is ok
but when i go to www.example.com in my browser it redirect me to
https://example.com/404.php
what is wrong in my code!?

use .htaccess like this
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
RewriteRule ^index\.php(.*)$ /$1 [R,L,QSA]
ErrorDocument 404 404.php
first remove www if exists,
then redirect to https,
and finally remove index.php if exists
dont forget
i user try http://www.example.com/index.php to access your site it will redirect 3 times :|

Related

Redirects aren't working, is there a conflict with the rules or order of them?

I need to redirect any incoming traffic that is not www or https, to www and https.
The one issue is trying to add one more rule that would handle all traffic that used to be at rootdomain.com/blog/rest-of-url-title and send that to the subdomain https://blog.rootdomain.com/rest-of-url-title
This is what I have in my root domain htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/blog/(.*)$ https://blog.rootdomain.com/$1 [R=302,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=302]
</IfModule>
Then within the blog subdomain directory htaccess I have this (to redirect any non https to https and the rest is Wordpress based)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
When trying to hit any url that is rootdomain.com/blog/rest-of-url-title it redirects to
https://www.rootdomain.com/index.php
Instead of redirecting to the blog subdomain, it just goes to www and since that url title doesn't exist for www i just get 404.
I had to go with mod_alias redirectmatch to get it working how i wanted
RedirectMatch 302 ^/blog/(.*)$ https://blog.rootdomain.com/$1
It's odd because the mod_rewrite line in my original htaccess in root had this:
RewriteRule ^blog/(.*) https://blog.rootdomain.com/$1 [R=302,L]
Which did not work on this particular server, but worked fine on any other servers I tested on. Maybe because of a subdomain/directory mapping issue?
Regardless, I know it's not good practice to mix mod_rewrite and mod_alias, this final worked for me:
RedirectMatch 302 ^/blog/(.*)$ https://blog.skodaminotti.com/$1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=302]
</IfModule>
Would still love to hear if anyone knows why the rewriterule wouldn't work.

Apache .htaccess force https and redirect to folder

I'm trying to force https in my production apache server while in the same time redirect only the root route '/' to a subfolder '/front', so redirection to subfolder works fine but not https forcing.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^$ /front [L]
RewriteCond %{HTTP_HOST} !=localhost [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R]
I finally found the solution :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectoryIndex /front/index.html
RewriteCond %{HTTP_HOST} !=localhost [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R]

Redirect all traffic to bare domain SSL using htaccess

I'm trying to redirect all traffic through HTTPS and here's my htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -Indexes
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
RewriteBase /
RedirectMatch 301 /index.php/(.*)$ /$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Currently here's what's going on,
If the user types in www.domain.com it redirects to: https://domain.com which is good. I would like help in configuring the file so that if the user also types in domain.com they get redirected to https://domain.com
It would be as simple as updating your pattern:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

.htaccess redirect if domain = x.example.com

I have a site on a subdomain:
x.example.com
I put it on a subdomain because it's running on a separate server from the main site.
Using proxy for apache, I have now moved the site to example.com/x/ I want to make a 301 redirect from the subdomain like this:
Redirect 301 / http://example.com/x/
but I don't know how to make sure that it does not result in a redirect loop, as the page i'm redirecting to contains the same htaccess-file (as it's a proxy of the old file)
this is what I tried:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/ [R=301,L,NE]
this is my full .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^zenqa.herokuapp.com$ [NC]
RewriteRule ^(.*)$ http://staging.zenconomy.se/faq/$1 [R=301,L,NE]
</IfModule>
You're missing a backreference, you need a $1 to get the original request and pass it along to the redirect:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/$1 [R=301,L,NE]

Use Mod_Rewrite to Rewrite ROOT to Another Path

Note: I have tried the suggestions under this question but they either don't work or give me an infinite redirect loop error.
I have the following in my .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Rewrite old links to new
Redirect 301 /howitworks.php /how-it-works
Redirect 301 /testimonials.php /testimonials
Redirect 301 /contact_us.php /customer-service
Redirect 301 /affiliates.php /affiliates
Redirect 301 /account.php /customer/account
Redirect 301 /shopping_cart.php /checkout/cart
Redirect 301 /products.php /starter-kits
Redirect 301 /login.php /customer/account/login
# Force to Admin if trying to access admin
RewriteCond %{HTTP_HOST} www\.example\.com [NC]
RewriteCond %{REQUEST_URI} admin [NC]
RewriteRule ^(.*)$ https://admin.example.com/index.php/admin [R=301,L,QSA]
# Compress, Combine and Cache Javascript/CSS
RewriteRule ^(index.php/)?minify/([^/]+)(/.*.(js|css))$ lib/minify/m.php?f=$3&d=$2
# Always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
# Never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !server-status
# Rewrite everything else to index.php
RewriteRule .* index.php [L]
# Force www in url and non-example domains to example
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^ww2\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^qa\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^uat\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^local\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^admin\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L,QSA]
</IfModule>
I need a rewrite that will do the following:
Rewrite: http://subdomain.example.com/ to http://subdomain.example.com/page
I would also like this to hide the /page (/ would act as if it was /page) but this is not a necessary requirement. Also, I'd like it to work with HTTP or HTTPS.
Any help is greatly appreciated because I have been struggling with this for hours.
A correct redirect of the root path is simply :
RewriteEngine On
RewriteRule ^/$ /page [R]
to force this only on one subdomain (which should barely happen if your rewrite block is in a virtualhost definition) :
RewriteEngine on
RewriteCond %{HTTP_HOST} =subdomain.example.com
RewriteRule ^/$ /page [R]
Copying my deleted response:
In htaccess of your documentroot simply:
RewriteEngine on
RewriteCond %{HTTP_HOST} =subdomain.example.com
RewriteRule ^$ /page
You have two options to rewrite your root to another path,
DirectoryIndex:
DirectoryIndex anotherPath
Mod-rewrite:
RewriteEngine on
RewriteRule ^/?$ /anotherPath [L]
You can use one of the above options to internally redirect example.com/ to example.com/anotherPath.
References:
https://httpd.apache.org/docs/current/mod/mod_dir.html
http://httpd.apache.org/docs/current/mod/mod_rewrite.html