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

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.

Related

.htaccess redirect to HTTPS not working

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]

Good profile urls using .htaccess

I've three url types which are as follows:
http://www.mywebsite.com/myprofile.php?user_username=username
http://www.mywebsite.com/input.php?user_username=username
http://www.mywebsite.com/users.php?user_username=username
Currently, users have to type the whole address like (http://www.mywebsite.com/myprofile.php?user_username=username) to go to their profiles and the same with input and users.
What I want is if a user types http://www.mywebsite.com/profile/username, he would be automatically redirected to http://www.mywebsite.com/myprofile.php?user_username=username.
When a user types http://www.mywebsite.com/input/username, he would be redirected to
http://www.mywebsite.com/input.php?user_username=username.
When a user types http://www.mywebsite.com/username, he would be redirected to http://www.mywebsite.com/users.php?user_username=username.
I know that this can only be achieved through .htaccess. However, I've searched but with no fruitful result.
Any help please!
Update
I think that the following code is close to being correct. However, I'm not getting the correct result and the css is also getting messed a lot. Individually they are working but together they are messing up. Any help please.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /myprofile\.php\?user_username=(.*)\ HTTP
RewriteRule ^ /myprofile/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ myprofile.php?user_username=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /user\.php\?user_username=(.*)\ HTTP
RewriteRule ^ //%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php?user_username=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /input\.php\?user_username=(.*)\ HTTP
RewriteRule ^ /input/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ input.php?user_username=$1 [L]
Have it this way:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+input\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /input/%1? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+myprofile\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+user\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^input/([^/]+)/?$ input.php?user_username=$1 [NC,QSA,L]
RewriteRule ^profile/([^/]+)/?$ myprofile.php?user_username=$1 [NC,QSA,L]
RewriteRule ^([^/]+)/?$ users.php?user_username=$1 [NC,QSA,L]
Add these to the .htaccess file in your DOCUMENT_ROOT
RewriteEngine On
RewriteRule ^([^/]+)/?$ users.php?user_username=$1 [DPI,L]
RewriteRule ^input/([^/]+)/?$ input.php?user_username=$1 [DPI,L]
RewriteRule ^profile/([^/]+)/?$ myprofile.php?user_username=$1 [DPI,L]
Tested in Apache 2.2 and 2.4 :)
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

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.

.htaccess stop removing index.php

I am having trouble after switching a customers site over to my linux cpanel webserver
accessing the site does work but the links do not,
I need it to stop it removing index.php after the domain name on all of the links (or start adding it!)
This is currently my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Can anybody tell me how I can achieve this?
Thanks in advance
Change
RewriteRule ^(.*)$ /index.php/$1 [L]
to
RewriteRule ^(.*)$ /index.php/$1 [R=301,L]

Codeigniter htaccess to remove index.php and www

I'm trying to get my htaccess rewrite rules to remove the index.php from the url AND also redirect the www. requests to the non-www version.
This is my htaccess which works fine with removing the index.php:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And I came across another question on how to remove the www part:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But I just cant seem to get them to play nicely together! Any advice/suggestions most appreciated!
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteEngine On "starts" rewriting. RewriteCond and RewriteRule work as pairs.
Here, we send user to non-www version first, and cleans URLs.
You first need to make sure that the "non-www" rewrite rule comes first and then the one that will redirect all requests to the CodeIgniter bootstrap file. Do note that the following code will honor only HTTP rewrites. If you also need HTTPS it requires some modifications.
RewriteEngine on
# Sending all www requests to the non-www version.
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
# Now the CodeIgniter part
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Categories