Force HTTPS on my rewrite and rule .htaccess file - .htaccess

I would like to force https on my site but that it is compatible with the current rule of my .htaccess
Here is my htaccess file at the moment :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Thanks

Could you please try following, where you need to look for condition if variable %{HTTPS} is on or not, add this condition with already existing condition. Also please don't mix it with already existing URL as it may get expose to client/browser(taking example from your shown current example).
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Related

How To Remove .cgi From URL With .htaccess?

I use a .cgi file to run python on my server. However, whenever someone goes to the site, it brings them to https://example.com/main.cgi/ (main.cgi is the name of the cgi file). Is there a way to remove this from the .htacess file, so it goes to https://example.com/ but still runs the cgi file?
This is the current .cgi file:
RewriteEngine On
RewriteBase /website
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /main.cgi/$1 [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
How can I make a RewriteRule that makes the URL / go to /main.cgi/ without it appearing in the URL?
You should make your https implementation as a first rule of your .htaccess file, could you please try following. Written as per your shown samples, please make sure you clear your cache before testing your URLs.
RewriteEngine ON
##Rule for applying https to non-http calls.
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##Look for if any page doesn't have `/`slashes at the end then add it.
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1/ [R=301,L]
##Rules for non exiting file or directory to main.cgi file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ main.cgi [L]
NOTE: Using RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] personally gives me 1 slash at end so I had written a separate rule for adding trailing / for this one, to avoid 2 times / at the end of url.

Url rewriting rule with .htaccess file

I'm here to ask for advice about the .htaccess file that I can't configure as I would like.
I have a site, let's say :
www.mysite.com/php/blog.php
I would like the visitor to see in the address bar only:
www.mysite.com/blog
This is what the .htaccess file contains:
Options +FollowSymlinks
Options All -Indexes
RewriteEngine On
RewriteRule ^$ index.php #This line is to hide index.php on the home page, maybe it's not the right way to do it
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^php/([0-9a-zA-Z]+).php$ $1 [L] #I tried to write this, but it doesn't work
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I suspect that this question has been asked many times, but I have really tried, without success.
Can you help me, please?
Thanks !
Your patter is wrongly expecting URI to start with /php/ which is not the case here. Also keep http -> https rules at top and then handle .php extension rule like this:
RewriteEngine On
# http -> https redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# To externally redirect /php/file.php to /file
RewriteCond %{THE_REQUEST} \s/+(?:php/)?(\S+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward file to /php/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php/$1.php -f
RewriteRule ^(.+?)/?$ php/$1.php [L]

Redirect to anothe page using .htaccess

I want to redirect the url of http://sitename/modules/fb/fb.php to http://sitename/modules/take_control .How can i do that using htaccess
I tried like
RewriteRule ^/?modules/fb/fb\.php$ take_control , Is that right ?
Your solution is the opposite of what you most likely want if you are trying to rewrite to take_control.
Give this a try. These rules should provide the rewrite with take_control as the URI both ways.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /+modules/fb/fb\.php [NC]
RewriteRule ^ /take_control/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^take_control/?$ /modules/fb/fb.php [L]

SEO-friendly URL rewrite in .htaccess

Before you flag this question -- I did search for answers, and found one close to mine that was not answered directly, so...
I am trying to write a simple RewriteRule in my .htaccess file to change ONLY the links that match to an SEO-friendly format.
RewriteEngine On
RewriteRule ^/includes/seo.([a-zA-Z]+).php$ /$1
So if I have a file
/includes/seo.mydocument.php
it will appear in the browser as
/mydocument
Not sure what I'm missing.
Leading slash is not matched in .htaccess rules so use this
RewriteRule ^includes/seo\.([a-zA-Z]+)\.php$ /$1 [L,R]
But probably you want this:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+includes/seo\.([^.]+)\.php[/\s?] [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /includes/seo.$1.php [L]

.htaccess not working properly

I changed .htacess file as below.
RewriteCond %{REQUEST_URI} (.*)product-list.php(.*)
RewriteRule (.*) www.example.com/swimming-pool/product-list\.php$1 [R=301,L]
RewriteCond %{REQUEST_URI} (.*)product-info.php(.*)
RewriteRule (.*)\?(.*)$ www.example.com/swimming-pool/product\-info\.php$2 [R=301,L]
i just need that when i request for
http://www.example.com/product-info.php?Applepc.html should be redirected to
http://www.example.com/swimming-pool/product-info.php?Applepc.html
Output is getting like this in URL field:-
www.example.com/swimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpproduct-list.php?Flowers-pg1-cid38.html
Please tell me where i am mistaking.
Although you don't mention what is the purpose of the other rule with product-list.php, it is included in this rule-set.
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (product-list|product-info)\.php [NC]
RewriteRule ^(.*)/?$ swimming-pool/$1 [R=301,L]
Redirects permanently any URL like this one
http://www.example.com/product-info.php?query or
http://www.example.com/product-list.php?query
To
http://www.example.com/swimming-pool/product-info.php?query or
http://www.example.com/swimming-pool/product-list.php?query
It seems the problem with the actual rules is they are generating a loop.

Resources