I have this in my .htaccess file
RewriteEngine on
#
#Sub-dir e.g: /cmsms
RewriteBase /
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# but ignore POST requests.
#RewriteCond %{REQUEST_URI} !/$
#RewriteCond %{REQUEST_URI} !\.
#RewriteCond %{REQUEST_METHOD} !POST$
#RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
How can i Redirect my website from http to https and also avoid index.php?page= from the pages?
You can do:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
Related
I wish to remove a trailing slash when one is given using htaccess. What would be the best way to do it that will work with my existing rules as below:
# make sure www. is always there
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
A sample URL would be something like:
https://www.example.com/this-test/
I of course want the ending slash removed.
I've tried this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]
But that does not work with the existing rules that are there. It ends up redirecting to the index.php pages due to the other rules.
Have it this way:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
# if requested url does not exist pass it as path info to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [QSA,L]
Make sure to test it after completely clearing browser cache.
I'm using these .htaccess commands to add a trailing slash at the end of my URLs.
# Redirect To Trailing Slashes If Not A Folder Or A File...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
Its working great, however it breaks my laravel email verification because this URL
http://website.test/email/verify/12585/d3942dce589a8baf879be01b717184712b119a72?expires=1630646035&signature=59832e4888913f960ceec9c96e01d15bec742446bce834c37d4e0e8d67241c26
Becomes this URL (It adds a trailing slash at the end of the URL)
http://website.test/email/verify/12585/d3942dce589a8baf879be01b717184712b119a72/?expires=1630646035&signature=59832e4888913f960ceec9c96e01d15bec742446bce834c37d4e0e8d67241c26
👆
How to add an exception for this particular URL?
Full .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect To Trailing Slashes If Not A Folder Or A File...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# Redirect Trailing Slashes If Not A Folder...
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} (.+)/$
# RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
You can add an exception in your rule:
# Redirect To Trailing Slashes If Not A Folder Or A File...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteCond %{THE_REQUEST} !\s/email/verify/ [NC]
RewriteRule . %{REQUEST_URI}/ [R=301,L,NE]
# remove trailing / for /email/verify/...
RewriteCond %{THE_REQUEST} \s/email/verify/ [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L,NE]
Make sure to clear your browser cache completely.
I want that if somebody hits this url:
https://sk.carpul.eu/search?t=0&fp=Bratislava&fla=48.1486&fln=17.1077&tp=Zvolen&tla=48.5762&tln=19.1371&sdate=&stime=&p=1&qh=4b9873ddfb
It will have nice places in url and search rename to spolujazda as:
https://sk.carpul.eu/spolujazda/Bratislava/Zvolen/?t=0&fp=Bratislava&fla=48.1486&fln=17.1077&tp=Zvolen&tla=48.5762&tln=19.1371&sdate=&stime=&p=1&qh=4b9873ddfb
But still server will point to:
https://sk.carpul.eu/search?t=0&fp=Bratislava&fla=48.1486&fln=17.1077&tp=Zvolen&tla=48.5762&tln=19.1371&sdate=&stime=&p=1&qh=4b9873ddfb
I tried in htaccess:
# check if the actual request if for "this1"
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?fp=([^&]+)&tp=([^&\ ]+)
# redirect to "this2"
RewriteRule ^ spolujazda/%1/%2/? [R=301,L,NE]
# now rewrite "this2" back to "this1"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?search/([^/]+)/([^/]+)/?$ /search.php?fp=$1&tp=$2 [L,QSA]
But not working.
For .php removal I have this in the beginning of htaccess:
Options +FollowSymlinks
RewriteEngine On
## hide .php extension
# To internally forward /dir/file to /dir/file.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
You may use these rules in your site root .htaccess:
RewriteEngine On
# check if the actual request if for "this1"
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?(?:.*&)?fp=([^&]+).*&tp=([^&\s]+) [NC]
# redirect to "this2"
RewriteRule ^ /spolujazda/%1/%2/ [R=301,L,NE]
# now rewrite "this2" back to "this1"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(?:spolujazda|search)/([^/]+)/([^/]+)/?$ /search.php?fp=$1&tp=$2 [L,QSA]
We moved the the whole site from http:// to https:// this works fine if you arrive at the site via https://. However all the previously cached urls (in Google) on http:// need to be redirected. Also we had previously been using a rewrite to take out .php in the urls and want to keep this. But this is where we are having problems.
The redirect we have from http to https pages in whole site which works with the htaccess below, but it does not remove the .php extension on those redirects.
Have tried these
Force HTTPS on certain URLs and force HTTP for all others
Here is the problem
Here is an example old url, see how it get rewriiten with .php not removed.
http://www.myitalianliving.com/product/1105/translucent-stackable-indoor-outdoor-chair-cooler-by-scab
Here is the site
https://www.myitalianliving.com/
Here is the .htaccess file
RewriteEngine on
## provide ip address redirect to canonical and SEO puproses
## see: https://stackoverflow.com/questions/9985735/redirect-ip-to-domain
#
#RewriteCond %{HTTP_HOST} ^81\.29\.78\.50$ [NC,OR]
#RewriteCond %{HTTP_HOST} ^([a-z.]+)?myitalianliving\.com$ [NC]
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{SERVER_PORT} 80
#RewriteRule ^(.*)$ https://www.myitalianliving.com/$1 [R=301]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.myitalianliving.com/ [R=301]
RewriteRule ^index\.html$ https://www.myitalianliving.com/ [R=301]
RewriteRule ^index\.htm$ https://www.myitalianliving.com/ [R=301]
## added below as in testing we had just the .com/index in the url no extension.
RewriteRule ^index https://www.myitalianliving.com/ [R=301]
#
##########################
# Try to remove .php AND send to https from http connection
##########################
Options +SymlinksIfOwnerMatch +MultiViews
#
#### RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##### RewriteRule ^(.*).php/(.*) $1.php?$2
RewriteRule ^(.*?).php/(.*?) $1.php?$2 [NC]
#
## https://stackoverflow.com/questions/4398951/force-ssl-https-using- htaccess-and-mod-rewrite
#
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You need to move your HTTPS redirect to the top of your set of rules:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
## provide ip address redirect to canonical and SEO puproses
## see: http://stackoverflow.com/questions/9985735/redirect-ip-to-domain
#
#RewriteCond %{HTTP_HOST} ^81\.29\.78\.50$ [NC,OR]
#RewriteCond %{HTTP_HOST} ^([a-z.]+)?myitalianliving\.com$ [NC]
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{SERVER_PORT} 80
#RewriteRule ^(.*)$ https://www.myitalianliving.com/$1 [R=301]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.myitalianliving.com/ [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html?\ HTTP/
RewriteRule ^index\.html?$ https://www.myitalianliving.com/ [L,R=301]
## added below as in testing we had just the .com/index in the url no extension.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\ HTTP/ RewriteRule ^index https://www.myitalianliving.com/ [L,R=301]
#
##########################
# Try to remove .php AND send to https from http connection
##########################
Options +SymlinksIfOwnerMatch +MultiViews
#
#### RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##### RewriteRule ^(.*).php/(.*) $1.php?$2
RewriteRule ^(.*?).php/(.*?) $1.php?$2 [L,NC]
Additionally, you need to add conditions to the index redirect rules and use the L flags (just in case).
Moving HTTPS redirect to the top worked with the following redirect at the bottom.
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
# Rewrite URLs for processing by router (article.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/(.+)$ article.php?url=$1 [QSA,L]
# Rewrite URLs for processing by router (product.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/(.+)$ product.php?url=$1 [QSA,L]
# Rewrite URLs for processing by router (page.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.+)$ page.php?url=$1 [QSA,L]
# Rewrite URLs for processing by router (section.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ section.php?url=$1 [QSA,L]
I've just installed an SSL on my server and enabled https on my websites admin area however now when normal pages are accessed with https:// instead of http:// I get a 404 page. This is my htaccess, it has the standard expressionengine index.php removal in as well but I can't work out what I need to change to make https:// work if front-end pages are accessed with it, any ideas?:
<IfModule mod_rewrite.c>
RewriteEngine On
# force https for all URLs in /admin.php
RewriteCond %{HTTPS} =off
RewriteRule ^admin.php https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Replace all of your code with this:
RewriteEngine On
# force https for all URLs in /admin.php
RewriteCond %{HTTPS} off
RewriteRule ^admin\.php https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !/system/ [NC]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]