Too many redirection CSS - apache

I obtain error style.css:1 GET https://www.example.com/cs_CZ net::ERR_TOO_MANY_REDIRECTS
Could be related to my htaccess?
RewriteEngine On
AddDefaultCharset utf-8
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
EDIT:
After #RavinderSingh13 changes I have error with permissions

Have your htaccess file in following way, please do make sure to clear your browser cache before testing your URLs.
RewriteEngine On
AddDefaultCharset utf-8
##Placing rules for non-https URLs here to apply https on URLs.
##Fixes: Added NE flag in rules here.
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##Placing rules for non www URLs here to apply www on URLs.
##Fixes: Added NE flag in rules here.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
##Rules for files to block those urls.
##Fixes: Added L flag to the rules.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [F,L]
RewriteRule ^\.htaccess/?$ - [F]
##Removed condition RewriteCond %{REQUEST_URI} ="" here as its not required.
RewriteRule ^/?$ /public/index.php [L]
##Rules for non public uris to handle here.
RewriteCond %{REQUEST_URI} !^/public [NC]
RewriteRule ^(.*)/?$ /public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(public)/.*$ /$1/index.php [QSA,NC,L]

Related

How to rewrite URL by Htaccess with basic parameters in core PHP

Hello all i am having a issue with this URL rewrite.
my current URL is like this
domain.com/professional?seo=ACCOUNTIFY-SERVICES-&-ADVISORS&id=3173
here the page name professional can be changed to any page like i will have the similer url with other page names.
Desired URL
domain.com/professional/ACCOUNTIFY-SERVICES-&-ADVISORS/3173
i know that i can do this by htaccess for sure
here is my htaccess file
RewriteEngine On
RewriteBase /
Options All -Indexes
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
# remove index.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Header set Access-Control-Allow-Origin "*"
please suggest me the changes needs to be done here to achieve the desired url.
Thank you so much for any help.
i have been reading some answers here and if i find a solution i will post here.
You can use these rules:
RewriteEngine On
RewriteBase /
Options All -Indexes
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([^.]+)(?:.php)?\?seo=([^\s&]+)&cat=([^\s&]+)&id=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+([^.]+)(?:.php)?\?seo=([^\s&]+)&id=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L,NE]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# internal forward from pretty URL to actual one
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?&seo=$2&cat=$3&id=$4 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/([^/]+)/([^/]+)/?$ $1.php?&seo=$2&id=$3 [L,QSA]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Header set Access-Control-Allow-Origin "*"

How to write clean url in reverse?

I have the following in my my htaccess
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^terms-and-conditions terms.php
RewriteRule ^privacy privacy.php
RewriteRule ^thank-you thanks.php
How could I also do this in reverse such as typing /privacy.php is rewritten as /privacy.
You need to use 3 separate rules using %{THE_REQUEST} variable to avoid redirect loop:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+terms\.php[\s?] [NC]
RewriteRule ^ /terms-and-conditions [R=301,L]
RewriteCond %{THE_REQUEST} \s/+privacy\.php[\s?] [NC]
RewriteRule ^ /privacy [R=301,L]
RewriteCond %{THE_REQUEST} \s/+thanks\.php[\s?] [NC]
RewriteRule ^ /thank-you [R=301,L]
RewriteRule ^terms-and-conditions/?$ terms.php [L,NC]
RewriteRule ^privacy/?$ privacy.php [L,NC]
RewriteRule ^thank-you/?$ thanks.php [L,NC]

How to resolve conflicting mod_rewrite rules and conditions

I have a couple of rewrite rules in my .htaccess that seem to be conflicting.
I am running an ExpressionEngine (CodeIgniter) website that resolves all URI's using an index.php file. I have removed the index.php from the URI's for aesthetic reasons.
What I am trying to achieve:
Redirect 301 all pages with a trailing slash (example.com/bla/ =>
example.com/bla)
Remove index.php from all URI's
What I have now:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond $1 !^(images|system|themes|index\.php|admin\.php|favicon\.ico|robots\.txt|humans\.txt|crossdomain\.xml) [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
</IfModule>
What works:
All deeplinks are redirected to their non-trailing-slash version
(example.com/bla/ => example.com/bla).
index.php is removed from all deeplink pages.
What does not work:
The homepage (example.com) gives me an error in Google Chrome, saying "Too many redirects".
How do I update the conditions and rules so that I achieve clean links without a trailing slash, and without index.php, regardless of which page it is.
Have it like this:
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule ^(.*?)index\.php(/.*)?$ /$1$2 [R=301,NE,L]
RewriteCond $1 !^(images|system|themes|index\.php|admin\.php|favicon\.ico|robots\.txt|humans\.txt|crossdomain\.xml) [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
Clear your browser cache before testing.

Rewrite all to https with htaccess and Magento

We want to redirect every page in Magento to https. We know how to change this in Magento, but we do not know how to do this in our current htaccess? For more information, please see our current htaccess file below, stripped from unnecessary comments.
Current htaccess:
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
DirectoryIndex index.php
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
RewriteCond %{THE_REQUEST} \s+/index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1? [L,R=301,NC,NE]
RewriteRule ^api/rest api.php?type=rest [QSA,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteRule ^home/?$ /? [R=301,L,NC]
# ADD WWW TO NONE WWW FOR BOTH HTTPS AND NONE HTTPS
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#REDIRECT ALL .HTML FILES AND ALL .HTML/ FILES WITH TRAILING SLASH
RewriteRule ^google[0-9a-f]+.html$ - [L]
RewriteRule (.+)\.html$ /$1/ [L,R=301]
RewriteRule (.+)\.html\/$ /$1/ [L,R=301]
# ADD TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# TRAILING SLASH CHECK
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
#CHECK IF REDIRECT POINTS TO A VALID FILE
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#REWRITE EVERYTHING ELSE TO INDEX.PHP
RewriteRule .* index.php [L]
Solved by adding:
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
Add the below code
RewriteEngine on
after
RewriteCond %{HTTPS} on
RewriteRule ^$ https://%{HTTP_HOST} [L,R]
And goto admin>SYStem>configuration>General>Web>
change all secure url and unsecure to https:
ex:

apache rewrite rule: http to www doesn't work

I tried to define a apache rewrite-rule to redirect users which type http://mydomain.net to get on my website to www.mydomain.net. So in the .htaccess-file I wrote the following:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
But this doesn't work: when I type mydomain.net I get in my browser
Forbidden
You don't have permission to access / on this server.
When I try the opposite
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [C]
RewriteRule ^www\.(.*)$ http://$1 [R=301,L]
that works (when I type in www.mydomain.net I get to mydomain.net). Does anybody know why and how to solve the problem?
Here is my .htaccess:
RewriteEngine On
# www to http
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
RewriteRule ^typo3$ - [L]
RewriteRule ^typo3/.*$ - [L]
RewriteRule ^uploads/.*$ - [L]
RewriteRule ^fileadmin/.*$ - [L]
RewriteRule ^typo3conf/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# naechste Zeile auskommentieren,
# wenn .html Endung in RealURL eingestellt ist
RewriteRule (.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R]
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule / %{REQUEST_URI}/index.html [L]
RewriteCond %{REQUEST_FILENAME}/index.htm -f
RewriteRule / %{REQUEST_URI}/index.htm [L]
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule / %{REQUEST_URI}/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
You should try it this way. You need a forward slash before $1. However I would do it this way so that it won't affect subdomains. Change the condition to check for the TLD instead of the absence of www.
RewriteCond %{HTTP_HOST} ^mydomain.net [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !\.([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Use the above code it should work and to fix the forbidden error bit you will need to add in the following bit of code in apache httpd.conf file :-
<Directory />
Require all granted
</Directory>
You can find more permission about the 403 error on an article I have written here : - http://codenathan.com/apache/you-dont-have-permission-to-access-the-requested-directory