htaccess get https for a folder excluded from rewrites rules - .htaccess

I'm using Laravel, so I have rewrite rules to redirect to the index.php, I've rules to redirect to www and to https too.
I want URI "/blog" not to be redirected to index.php but I need it like:
https://www.example.com/blog
actually it redirects to:
example.com/blog
becouse this rule RewriteCond $1 !^(blog) exludes it form rewrite rules
This are my rules:
<IfModule mod_rewrite.c>
RewriteBase /
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond $1 !^(blog) #I need this to get www and https but not further rules
# To https
RewriteCond %{REQUEST_SCHEME} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# To www if is not a subdomain
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

If you want blog/ to be skipped from index.php rewrite then change last rule to this:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^blog(/.*)?$ index.php [L,NC]

Related

How To Redirect www to Non-www with path url complete in apache2

All paths with www (www.example.com/posts/categories) redirect to the home page. How can i redirect from .htaccess and keep the full path?
https://www.example.com/posts/categories to https://example.com/posts/categories
current configuration:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
You've put this rule in the wrong place. It needs to go at the start, immediately after the RewriteEngine directive. By placing it at the end, after the rewrite to index.php, any request for the www subdomain will be redirected to index.php (during the second round of processing).
As a general rule, external redirects should always go before internal rewrites.
You should also consider implementing an HTTP to HTTPS redirect.

Rewritecond by `%{HTTP_HOST}` is ignored?

I have multiple domains that point to the same folder on my server. The code in index.php page recognizes which domain is accessing it and shows different content for each domain.
Now I want that www.domain-a.com\sitemap.xml opens /sitemap-a.xml so that each domain has its own sitemap.
I use the following rule in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]
However, if I access www.domain-a.com\sitemap.xml then the content from the file /sitemap.xml is shown instead of the file sitemap-a.xml.
Is the redirecting rule wrong? If so, how can I fix it?
I am using Laravel. This is the full .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]
# browser request: .php to non-php
# https://stackoverflow.com/questions/13222850/redirect-php-urls-to-urls-without-extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Can you try this rule just above # Handle Authorization Header line:
RewriteCond %{HTTP_HOST} (?:^|\.)domain-a\.com$ [NC]
RewriteRule ^sitemap\.xml$ /sitemap-a.xml [L,NC]

.htaccess redirect to HTTPS doesn't work correctly

Redirect works only on the first page, http://example.com to https://example.com.
But it doesn't work on subfolders: http://example.com/news or /news/category etc. redirects me to the https://example.com/index.php (My website is on Laravel 5.5).
Any suggestions pls?
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
# ensure !www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Header add Access-Control-Allow-Origin "http://glyphicons.com/"
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Remember to replace example.com and www.example.com with your actual domain name

How to remove ssl from specific url or page with htaccess

I am having a site with ssl. All pages from site are redirecting to https with www.
I want following urls to be non ssl, so it will work with http only.
www.domain.com/admin/order/remote_request?
www.domain.com/admin/order/print/131756
My current htacess file is ad follows
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Redirect non www request to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Redirect http request to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Try :
# Redirect http request to https
RewriteCond %{THE_REQUEST} !/admin/order/remote_request\? [NC]
RewriteCond %{THE_REQUEST} !/admin/order/print/131756 [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

301 Redirect won't work in Laravel

Lets say I'm trying to do a 301 redirect to /housing-associations from /table/09 - the following code is not redirecting. I'll include the whole .htaccess file. It's a Laravel installation. The other redirects work ie all urls are being directed to index.php and trailing slashes are being removed:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^fabric [NC,OR]
RewriteCond %{HTTP_HOST} ^www.fabric [NC]
RewriteRule ^/table/[0-9]{2} /housing-associations [R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Has anyone got any ideas. I cannot work out why this won't work?
Thanks
This should work for you:
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?fabric [NC]
RewriteRule ^table/[0-9]{2} /housing-associations [R=301,L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The two HTTP_HOST conditions are merged into one, where the www. is optional. The leading slash from /table has been removed. Lastly, the L flag was added to prevent the index.php rule from running. If the L flag is not there, you'll get a Moved Permanently message from Apache.

Resources