.htaccess redirect to HTTPS not working - php

RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
This is how my current .htaccess looks like at the moment. Line 3 and 4 I took from another question at stackoverflow, and the rest simply removes the .php file extension. But the redirect to https part isn't working. Any ideas?
EDIT:
I just figured out that the problem is in my site's .conf file in the sites-enabled folder of apache. I need a separate block for the :80 and :443 port, which I can't figure out how to do, but that's a topic for another question.

The following is what I use to enforce https, hope it will be of use.
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]

You can try this for redirect on https:
RewriteRule ^(.*)$ https://www.%1.%2/$1 [L,R=301]

The Following code may Works.
Url:- home
RewriteRule ^home$ index.php?page_slug=$0 [QSA]
RewriteRule ^home/$ index.php?page_slug=$0 [QSA]
This is URL- page/new
RewriteRule ^page/([a-zA-Z0-9-/]+)$ page.php?page_slug=$1 [QSA]
RewriteRule ^page/([a-zA-Z0-9-/]+)/$ page.php?page_slug=$1 [QSA]

You can use the following code to redirect from http to https :
RewriteCond %{HTTPS} ^off$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]

Related

How to force https using friendly url with $_SERVER['REQUEST_URI']?

folks!
I have an PHP application runing on Apache2 in localhost. The system uses friendly URL from $_SERVER['REQUEST_URI']. My .htaccess:
RewriteEngine On
#RewriteRule ^(.*)$ https://myapp.localhost/$1 [R=permanent]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
If I comment lines 3 and 4, the system run ok. If I use the code as I show you, he write URL as https://myapp.localhost/index.php?/ where it needs to write https://myapp.localhost/. SSL Certified it's ok.
Images and archives can't be found with this configuration, but without lines 3 and 4 and using https manually it's showed.
Thank you for your cooperation.
Sorry, the last message i answer in portuguese, Mr. #Martin.
Maybe the problem was use more than one tutorial. After so much searchs. I understand: it was necessary delete some lines of code at /etc/apache2/sites-available/bigpecasnew-localhost-ssl.conf and add this lines at <Directory /home/user/site>SSLOptions +StdEnvVars</Directory> as I read in
https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-apache-in-ubuntu-18-04.
After I change my code on .htaccess:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
So that works.
Thank you for your cooperation.

Does run .htaccess file of php website on windows server ? is that right way?

I developed a PHP website using wamp server, am hosting it on windows server.
I have a file name .htaccess which contains rewrite rule for URL. it works on my localhost but does not work on the windows server.
what will I do?
can I create a web.config file?
please help me, Sir/Madam, Thank you !!😃
can you show the .htaccess code?
or try this code on top of your .htaccess file.
use https in place of HTTP if have SSL
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
use this code
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^home index.php [NC,L]
RewriteRule ^login login.php [NC,L]
RewriteRule ^logout logout.php [NC,L]
RewriteRule ^category category.php [NC,L]
RewriteRule ^cat_id/([^/]*)$ /doha_classified/ad_list.php?cat_id=$1 [L]
RewriteRule ^ad_id/([^/]*)$ /doha_classified/search_an_ad.php?ad_id=$1 [L]
ok lets try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^index index.php [L,QSA]
RewriteRule ^home index [L,QSA]
for home page only if work adds for another one.

url rewriting not working on one.com provider

Currently i am trying to get url rewriting to work on one.com hosting provider which is a pain and support don't understand the issue, so i was thinking of asking here if you may assist me finding the issue.
I'm using this rewriterule which worked on localhost but not with the provider:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?key=$1&seo=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?key=$1&seo=$2
</IfModule>
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^(.*)$ http://www\.mysite.com/$1 [R=permanent,L]
and in index:
if($key=='post') //Cant include $seo here because the variable differ for
//each post on the site
{
include('post.php'); // Post page
}
Then in post.php ofc i take the seo GET variable to display the post content on the page.
complete url www.mysite.com/post/test-test-test
The error i end up with is 404
Not Found
The requested URL /post/test-test-test.php was not found on this server.
So basically what i understand is that it tries to enter a folder named /post/ and look for the file test-test-test.php
So i believe it doesn't include the rewrite rule, or can you find an error in the rewrite rule which worked on localhost?
You need to rearrange your rules and for adding .php extension make sure a file with .php exists:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
# ignore rest of the rules for files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)/?$ index.php?key=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?key=$1&seo=$2 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>

How to remove .php from all urls with no trailing slash?

I need to remove the .php from all the urls on my website, and if someone types in a full url it needs to redirect to the php-extension-free version. Eg:
website.com/about.php -> website.com/about
I need to do this in as SEO friendly a manner as possible, so I'm guessing that would be a 301 redirect so Google and others know that the page has a new location at the new php-extension-free URL. Any dup content would also have to be avoided, so it's important that the page isn't accessible at both the old and new urls, which is what would happen if all I did was a simple:
RewriteRule about about.php [L]
I've seen a number of .htaccess approaches for this, but they all seem to add a trailing slash to the URL. It's important that this doesn't happen too.
Also, I have an explicit HTTPS redirect happening for a couple pages, and I need to make sure I don't create a redirect loop. Here's the code that's currently redirecting those pages to HTTPS.
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2).php$ https://website.com/$1.php [R=301,L,QSA]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2).php
RewriteCond %{REQUEST_URI} !^/(.*)\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://website.com/$1 [R=301,L,QSA]
You can use this code in your root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2)/?$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2)
RewriteCond %{REQUEST_URI} !\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
The below answer can be found here
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

How to delete a subdirectory from a URL?

So I'm trying to turn my website from this:
http://profe5.com/Profe5-Web/public_html/login.html
To this:
http://profe5.com/login
I've been struggling to do this, but whenever I run it I get 404 error!
This is my htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [R=301,L]
RewriteRule ^Profe5-Web/public_html/(.*)$ $1 [R=301,L]
It would be so awesome if you guys could help me!
Thanks so much!
This should be your complete .htaccess:
DirectoryIndex Profe5-Web
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^profe5\.com$ [NC]
RewriteRule ^(.*)$ http://profe5.com/$1 [R=301,L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+Profe5-Web/public_html/([^.]+)\.html [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ Profe5-Web/public_html/$1.html [L,QSA]
you might consider instead doing this via a virtual host; check with your hosting company about setting it up
If you want /login to map to Profe5-Web/public_html/login.html - try this:
RewriteRule ^([^.]+)$ Profe5-Web/public_html/$1.html [R=301,L]
The RewriteRule you already have, RewriteRule ^([^\.]+)$ $1.html [R=301,L] will map /login to /login.html, which doesn't seem to do what you need it to.

Categories