Add Trailing Slash to Posts - regex

I'm using following .htaccess code to add trailing slash all urls but homepage.
## Base Redirects ##
# Turn on Rewrite Engine
RewriteEngine On
# Include trailing slash on non-filepath urls
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ https://hamilekadin.net/$1/ [R=301,L]
# Remove trailing slash from directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/$ https://hamilekadin.net/$1 [R=301,L]
# Force HTTPS and remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://hamilekadin.net/$1 [R=301,L]
I want non-www url's, https protocol and trailing slash after post and page url's.
With this .htaccess I'm getting 404 error on categories, pages, posts.
Also my permalink type is: /%postname%/

Here is the code for trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://yourdomainname.com/$1 [L,R=301]
For non-www url,there are two option
Option 1 :
You need to run SQL query to remove www from url
Option 2
Add following code in .htacess
RewriteCond %{HTTP_HOST} ^www. yourdomain\.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com $1 [L,R=301,NC]
According to me to remove www most preferable way is option 1 where you just need to run the query.

Related

How to remove index.php from URL path except if the path is /index.php/

I need to remove index.php from inner pages so that the URL path like /index.php/path would be just /path. But at that if the path is /index.php/ then not to remove index.php and leave everything as is. I try the following lines in my .htaccess:
RewriteCond %{REQUEST_URI} !^.*/index.php/$ [NC]
RewriteRule /index.php/(.+)$ /$1
But they remove index.php always including when the path is /index.php/
You can use these 2 rules in your site root .htaccess:
RewriteEngine On
# external redirect to remove /index.php
RewriteCond %{THE_REQUEST} \s/+index\.php/(\S+) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# rewrite to /index.php/<uri>
RewriteRule .+ index.php/$0 [L]

htaccess redirect to https for an already Re-Written URL

This is my htaccess
Options +FollowSymLinks
RewriteEngine On
# redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# redirect to HTTPs for register
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} register
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
# don't do anything for images/css/js etc
RewriteRule \.(gif|jpe?g|png|css|js|woff|map)$ - [NC,L]
# redirect to HTTP for all other pages
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} !register
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [PT,L]
I've got a register page with an URL like hxxp://www.example.com/index.php?register and the htaccess is redirecting correctly to HTTPS.
Now I also want my buy page(hxxpp://www.example.com/buy) to redirect to HTTPS. This page's real URL is hxxp://www.example.com/index.php?buy
Please advice me on how to modify my htaccess rule to take care of this need.
You can use regex alternation in your https rule:
# redirect to HTTPs for register
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /(register|buy)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
Make sure this is very first rule in our htaccess.

Getting .htaccess file to work in subfolders

I finally figured out how to get certain things to work on my website using the .htaccess file.
Redirect all non-www requests to www version. DONE.
Remove all php file extensions and add a trailing slash. DONE.
Prohibit directory views. DONE.
Limit caching. DONE.
Redirect 404 requests to home page. DONE.
This all seems to work well, but only in the ROOT directory.
It doesn't work well in subfolders. PHP extensions aren't removed. Folder paths in URLs disappear.
As I'm new to .htaccess files and regular expressions, and getting to this point took some time and lots of trial and error, I'm hesitant to tamper with the code any further.
I would appreciate any guidance on:
How to optimize this file for subfolders.
How to optimize this file in general.
Thank you.
RewriteEngine On
# redirect non-www requests to www version
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteRule ^(.*)$ https://www.example.net/$1 [R=301,L]
# remove .php file extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
# disable directory view on web pages
Options -Indexes
# cached pages will expire in 5 days
ExpiresActive On
ExpiresDefault "access plus 5 days"
# re-direct 404 pages to home page
ErrorDocument 404 /
Keep your DocumentRoot/.htaccess like this:
# disable directory view on web pages
Options -Indexes
# cached pages will expire in 5 days
ExpiresActive On
ExpiresDefault "access plus 5 days"
# re-direct 404 pages to home page
ErrorDocument 404 /
RewriteEngine On
RewriteBase /
# redirect non-www requests to www (both http and https)
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2/ [R=302,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Apache non www to www badly re writting arrays on URL

we have the following .htaccess configuration:
RewriteEngine on
allow from all
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php
RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain1\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^/?$ "http\:\/\/www\.domain1\.com\/" [R=301,L]
Between other things we want to redirect all non-www URLs to the ones with the www.
Everything works fine, except for the pages where the URL is like this:
index?Form%5bplace%5d=Caribbean&Form%5bdestination%5d=Virgin+Islands&Form%5btype%5d=A
When we enter the URL without the www our redirect ends up with the following URL:
index?Form%255bplace%255d=Caribbean&Form%255bdestination%255d=Virgin+Islands&Form%255btype%255d=A
Which gives an 404 error because it is not recognized.
Any idea how to avoid this?
Replace your code with this:
allow from all
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# if a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php [L]
RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^domain1\.com$
RewriteRule ^/?$ http://www.domain1.com/ [R=301,L]
What's happening is that the % symbol is getting escaped to %25.
You can avoid this using the NE flag on your rules

Using htaccess to force a trailing slash before the ? with a query string?

I have the following in my htaccess file:
RewriteEngine On
RewriteBase /
# Check to see if the URL points to a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Trailing slash check
RewriteCond %{REQUEST_URI} !(.*)/$
# Add slash if missing & redirect
RewriteRule ^(.*)$ $1/ [L,R=301]
# Check to see if the URL points to a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Send to index.php for clean URLs
RewriteRule ^(.*)$ index.php?/$1 [L]
This does work. It hides index.php, and it adds a trailing slash... except when there is a query string.
This URL:
http://example.com/some-page
gets redirected to:
http://example.com/some-page/
but this URL:
http://example.com/some-page?some-var=foo&some-other-var=bar
does not get redirected. I would like for the above to be sent to:
http://example.com/some-page/?some-var=foo&some-other-var=bar
I've reached the limits of my understanding of redirects with this. If you have a working answer, I would really appreciate a walkthrough of what every line is doing and why it works. Double bonus awesomeness for an explanation of why what I have right now doesn't work when there is a query string involved.
Try adding a [QSA] to the end of the last Redirect rule to preserve the original query string as below
# Send to index.php for clean URLs, preserve original query string
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
a walkthrough of what every line is doing and why it works.
See my comments below
#turn mod_rewrite engine on.
RewriteEngine On
#set the base for urls here to /
RewriteBase /
### if the is not a request for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### and the URI does not end with a /
RewriteCond %{REQUEST_URI} !(.*)/$
### redirect and add the slash.
RewriteRule ^(.*)$ $1/ [L,R=301]
### if the is not a request for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite to index.php passing the URI as a path, QSA will preserve the existing query string
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
I believe that if you change this:
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
to this:
RewriteCond %{REQUEST_URI} !^([^?]*)/($|\?)
RewriteRule ^([^?]*) $1/ [L,R=301]
then it should do what you want.
The changes I made are:
In both rewrite-condition and -rule, I changed (.*) and ^(.*) to ^([^?]*), to ensure that, if there's a query-string, then it is not included in either regex. ([^…] means "any character that is not in …", so [^?] means "any character that is not a question mark".)
In the rewrite-condition, I changed $ to ($|\?), so as to match either end-of-URL or end-of-part-before-the-query-string.
In the rewrite-rule, I dropped the $, since it was no longer needed.