Add slashes to .php file extensions only - .htaccess

my htaccess codes add a slash to the end of all URLs but I just want it to add slashes to files ending with the .php file extension how can I do that?
my htaccess codes :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^\/(.*?)\/$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

Could you please try following. Written with shown samples only. Please make sure to clear browser cache before testing your URLs
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.php$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/seo$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

Related

Multiple htaccess rewrite rules

I'm using the below htaccess file.First one is working fine.But the second one is not working.
RewriteEngine On
RewriteBase /Epropertiesnew/
RewriteCond %{THE_REQUEST} /Properties(?:\.php)?\?project=([^&]+)&property=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /ViewNewProjects(?:\.php)?\?newproject=([^&]+)&url=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([A-Z,0-9-]+)/([A-Z,0-9-]+)/?$ Properties.php?project=$1&property=$2 [NC,L,QSA]
RewriteRule ^([A-Z,0-9-]+)/([A-Z,0-9-]+)/?$ ViewNewProjects.php?newproject=$1&url=$2 [NC,L,QSA]
If I commented the first one,second one is working fine.Please help me to fix this.

remove .php extension from filename containing dashes

For removing .php extension form URL with filename containing dashes like
http://www.sitename.com/here-the-filename-with-dashes.php
http://www.sitename.com/filename.php
to
http://www.sitename.com/here-the-filename-with-dashes
http://www.sitename.com/filename
I have tried several number of code already but no use via htaccess file.
Here HTACCESS FILE:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
#RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
#RewriteRule ^(.*?)/?$ $1.php
RewriteRule ^(.*)$ $1.php
#RewriteRule ^(.*)$http://www.sitename.ca/?$1.php [L,R=302]
#RewriteRule ^([^\.]+)$ $1.php [NC,L]
#RewriteRule ^([a-z]+)\/?$ $1.php [NC]
#RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{HTTP_HOST} ^sitename.com [NC]
RewriteRule ^(.*)$ http://www.sitename.com/$1 [L,R=301]
RewriteRule ^index.php$ http://www.sitename.com/ [R=301,L]
You are on the right path.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sitename.com$ [NC]
RewriteRule ^ http://www.sitename.com%{REQUEST_URI} [L,R=301]
RewriteRule ^index.php$ http://www.sitename.com/ [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php [NC]
RewriteRule ^ /%1 [QSA,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]

.htaccess redirect with php slug

My rewrite code is:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^products/([a-z0-9_-]+)$ products.php?slug=$1
But, this shows and internal server error for the products/slugname rewrite rule.
I've only used small letter, numbers and dash(-) in my slug id.
Try this :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ /$1.php [NC,L]
RewriteRule ^products/([a-z0-9_-]+)$ /products.php?slug=$1 [NC,L]

htaccess rewrite get url section not working

I am trying to rewrite the url example.com/parts/toolbucket/part.php?id=1 into example/toolbucket/part/1
I have most of the .htaccess working but the last part of the rewrite doesn't work.
This is my .htaccess
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/parts/([^\s]+)\s [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /parts/$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteRule ^toolbucket/([0-9]*)$ ./part.php?id=$1
Any ideas?
Because you are missing the directory part in the rule. If you want your URL to be this example.com/toolbucket/part/1
Replace this
RewriteRule ^toolbucket/([0-9]*)$ ./part.php?id=$1
With this.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^toolbucket/part/([0-9]*)/?$ /parts/toolbucket/part.php?id=$1 [L]

Htaccess redirect with many rules

I got rewrite rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|static)\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301] //add www before adress
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] //remove .php
RewriteRule ^ %1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-d //remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php
RewriteRule ^portfolio/([0-9]+)([^.]*)$ /portfolio-single.php?id=$1 [L]
But the last one dosent work, i want to have adress like www.mysite.com/portfolio/3-some-name => /portfolio-single.php?id=3
I dont know how to get i work.
(Sorry for my bad english)
Change order of rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|static)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteRule ^portfolio/([0-9]+) /portfolio-single.php?id=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Resources