Error in ridirecting HTTP requests to HTTPS using .htaccess - .htaccess

i already have .htaccess file in root directory with this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ list.php?user=$1 [L]
Now, i also want to ridirect all HTTP traffic to HTTPS pages using this htaccess file without removing existing codes, i tried some codes but i see error by browsers such as "Not Ridirecting Properly".
plz tell me some code according to the existing .htaccess file.
Thanks A Lot

If you need to do it with .htaccess, do it like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
You can also do it easily with a separate VirtualHost:
<VirtualHost *:80>
ServerName yourname.example.com
RewriteEngine On
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
ServerName yourname.example.com

Related

AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error

I keep seeing this error in my apache log when someone visits the shop page on my site, I have tried removing my htaccess and using .php?s=$var and I still get the same error so I think its something to do with the apache config files and not anything to do with the htaccess file, here it is anyway
htaccess:
RewriteEngine on
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{THE_REQUEST} //
RewriteRule ^(.*)$ /$1 [L,R=301]
RewriteRule ^home/?$ home.php [NC]
RewriteCond %{THE_REQUEST} /shop\?c=([^&\s]+)&s=([^&\s]+)&s2=([^&\s]+)&b=([^&\s]+) [NC]
RewriteRule ^ shop/%1/%2/%3/%4? [L,R=302,NE]
RewriteRule ^shop/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ shop.php?c=$1&s=$2&s2=$3&b=$4 [L,QSA,B]
RewriteRule ^shop/([^/]+)/([^/]+)/([^/]+)/?$ shop.php?c=$1&s=$2&s2=$3 [L,QSA,B]
RewriteRule ^shop/([^/]+)/([^/]+)/?$ shop.php?c=$1&s=$2 [L,QSA,B]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
SSL apache conf:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
HTTP apache conf:
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/example.com">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://www.example.com%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
There's nothing in the server config files you've posted that would result in a rewrite loop - there are no rewrites in your server config.
I have tried removing my htaccess and using .php?s=$var and I still get the same error
That is puzzling. Do you have any other .htaccess files in subdirectories? Without any .htaccess files and with the server config you've posted it would seem to be impossible to get a rewrite loop - since you would have no rewrites at all!
However, your last rule block that appends the .php extension could result in a rewrite loop under certain conditions. This needs to be corrected. You currently have:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
The problem with this is that the 2nd condition that checks to see that <filename>.php exists, is not necessarily performing the same file check that the following RewriteRule would rewrite to. eg. Request /foo/bar, where /foo.php exists, would result in a rewrite loop.
To resolve this issue, you would need to change the 2nd condition to something like the following instead:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
See my answer to the following ServerFault question that explains this in more detail:
https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error
So, the complete rule becomes:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ $1.php [L]
You should also ensure that MultiViews is disabled (if not already), since your URLs map directly to files less the .php file extension (otherwise mod_negotiation will rewrite the URL - before mod_rewrite - and you will lose the URL parameters). At the top of your .htaccess file include:
Options -MultiViews

How to redirect to https

I installed SSL and now I want to redirect my domain name to HTTPS . my .htaccess file has this configuration provided by my CMS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Options +FollowSymLinks
Options -Indexes
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . index.php [L,QSA]`
What CMS are you using? Most common CMS have plugins which will do a better job updating any non-HTTPS resources to HTTPS.
I know you requested no referral to another link but:
The Apache docs recommend against using a rewrite:
To redirect http URLs to https, do the following:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
</VirtualHost>
This snippet should go into main server configuration file, not into .htaccess as asked in the question.
This article might have come up only after the question was asked and answered, but seems to be the current way to go.
https://stackoverflow.com/a/21798882/11039985
If you must use htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Update: per your response, after making a backup of the htaccess file and try:
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . index.php [L,QSA]
I think what you are looking for is setting up hsts. You can find more about this header here. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security
This link will help in setting up hsts. https://www.globalsign.com/en/blog/what-is-hsts-and-how-do-i-use-it/

Redirecting & masking only one specific subdomain with .htaccess

I have the following .htaccess that is working perfectly for me. Now, I'm trying to mask just 1 subdomain with its folder. I've got everything set-up on the DNS end and right now when I type in app.domain.com it resolves to www.domain.com/app.
I need it to stay app.domain.com in the URL bar however, not just redirect to the folder.
I've been at this for 2 hours and can't figure it out. Anyone know what I should do?
Here's my .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{HTTP_HOST} !^app.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
If I understand the issue correctly, a user goes to http://app.example.com, you'd like that user to be redirected to http://app.example.com/app. Is that right? If that is the case, then simply add an alias on your virtual host so that example.com and app.example.com are both valid hostnames for that directory.
Find your httpd-vhosts.conf file or equivalent and configure your virtual host with something to the effect of the following example:
<VirtualHost *>
DocumentRoot /path/to/site/folder
ServerName www.example.com
ServerAlias app.example.com example.com
</VirtualHost>
Here is more information on the ServerAlias directive if you want to research it further.

Redirect loop when removing www in with RewriteEngine

Note: I have seen other questions/answers for redirecting www->non-www. This is more to know WHY this isn't working
So I have a VirtualHost file that looks roughly like this
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
...
RewriteEngine on
RewriteCond %{HTTP_HOST} www.(.+) [NC]
RewriteRule ^/(.*) http://%{SERVER_NAME}/$1 [R=301]
...
</VirtualHost>
But when I try it I keep getting a redirect loop. I don't see anything telling me what it's redirecting to in the access logs.
I suspect it keeps erroneously going to www.domain.com instead of to domain.com, even though I'm sure this should be correct...
Try this rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
You're probably getting into loop due to use of %{SERVER_NAME} variable.

http.conf redirect example.com/foo/bar to example.com/foo?id=bar

I am trying to create a dynamic page who's contents are determined by a variable passed to it, but I want the url to be nice. Basically I want to have a single index file in the directory /foo that handles any bar. example.com/foo/bar to example.com/foo?id=bar
I have tried everything I can think of in my httpd.conf file. My latest try was:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/foo/([a-z-]*) /foo/index.php?id=$1 [QSA,L]
However when I try to load example.com/foo/bar the page that loads is example.com
Any help is much appreciated!
Edit: It's still loading example.com/index.php when I visit example.com/foo/bar Maybe one of my other rules is interfering?
AllowOverride none
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)\.html$ http://www.example.com/$1.php [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/foo/([a-z-]*)(\/?)$ /foo/index.php?id=$1 [NC,QSA,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
.html to .php, what I'm trying to achieve, and example.com to www.example.com respectively.
I use this in my httpd.conf
<VirtualHost *:80>
DocumentRoot "/var/www/"
ServerName www.domain.com
ServerAlias www.domain.com
<Directory /var/www/>
#other directives
</Directory>
<Directory /var/www/foo/>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)(\/?)$ index.php?id=$1 [NC,QSA,L]
</Directory>
</VirtualHost>

Resources