I've got an SSL certificate, and I need to force https on my website.
Additionally, I need to remove trailing slashes, and hide the .php extension from files.
This is what I have so far, but it's causing too many redirects:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ https://hellorufus.com/$1 [R,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://hellorufus.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# 404 Error Template
ErrorDocument 404 /404.html
I've found a number of similar questions, but when I try to piece them together for my own needs, I end up with too many redirects again!
This is a version with a few corrections that appear plausible:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=302,L,QSA]
# Redirect external .php requests to extensionless url
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L,QSA]
# 404 Error Template
ErrorDocument 404 /404.html
Not sure if that already solves your issue. To be certain we would need some specific request URL that generates an endless rewrite loop.
Related
I want to be able to type “example.com/page” in the url and my browser to load “example.com/folder/page.php”
I literally looked at 10+ video tutorials and 20+ pages on how to do this and none of them are working properly. I can't include everything I've tried because I've tried so many with none of them working.
An explanation would also be helpful. Thanks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^folder/(.*)$ http://www.example.com/$1 [L,R=301]
Try this
# it may also need to set mutliviews
Options +MultiViews
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless URL
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless PHP URLs
RewriteRule ^([^/.]+)$ $1.php [L]
I have a problem for my php website, i do some research and my url look like this www.example.com/about, that's a correct output but i want to add extra forward slash so the url look like this www.example.com/about/ same with the wordpress behavior.
my .htaccess file:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
thanks.
I would like all possible URLs used to access my site to end up with a URL with no file extension and no trailing slash. Currently I have been able to solve the file extension issue, and the trailing slash issue separately, however if a URL with no file extension and a trailing slash is used it sends them to my 404 page, such as https://mysite/news/. This should end up as https://mysite/news and fetch the file news.php, but it redirects to my 404. Here is my current .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect to domain without www.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
# 404 errors
ErrorDocument 404 /404.php
# Remove file types
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
I'm trying to hide .php extension from my website while forcing trailing slash, I've been searching for the last few days with no success.
The site is running in a XAMPP server currently.
Here's the .htaccess file:
Options -Indexes -Multiviews +FollowSymlinks
RewriteEngine On
RewriteBase /
#removing .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
#forcing trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
ErrorDocument 404 http://localhost/404/
These links work:
localhost/about-us redirects to localhost/about-us/ which is correct but when I try to access localhost/about-us.php, it redirects to localhost/about-us/ instead of localhost/about-us/. (sorry can't post links)
How can this be fixed?
You just seem to have accidentally used an absolute path. Just remove the slash at the beginning of /$1/ like your other rules to make it work properly.
#removing .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ $1/ [L,R=301]
I want to access some php files which are located inside some subfolders. I'm using a .htaccess file, which removes all the .php extensions.
Here is the file:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.net/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.net/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
I have to say that I just copied this .htaccess file from a tutorial, because I have no experience in this area.
So a quick example: I can access www.example.net/folder/myphpfile.php , but I get a "file not found" error when I try to access www.example.net/folder/anotherfolder/myphpfile.php .
But it works when I delete the .htaccess file.
Can someone help me with this. Thanks!
Have it this way:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} \s/.+?\.php
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]