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]
Related
I am getting errors when I am trying to rewrite URL.
From this:
www.example.com/products/product-details.php?alias=abc-def-123
To this:
www.example.com/products/abc-def-123
Here is my .htaccess file
#Turn Rewrite Engine On
Options +FollowSymLinks -MultiViews
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([a-zA-Z0-9-]+)$ products/product-details.php?alias=$1 [NC,L]
</IfModule>
I am hitting this URL:
www.example.com/products/abc-def-123
Update:
With your shown samples please try following rules. Please make sure to keep your htaccess in your root folder along with your products folder(not inside it).
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)/?$ https://example.com/$1 [L,NE,R=301]
RewriteCond %{THE_REQUEST} \s/products/.*\?alias=(\S+)\s
RewriteRule ^ /products/%1? [R=301,L]
RewriteRule ^products/(.*)/?$ products/product-details.php?alias=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.*)/?$ $1.php [NC,L]
</IfModule>
Yeah!!!!!!!
I solved my problem myself and also I want to thanks #RavinderSingh13 to make effort.
Here is the whole .htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)/?$ https://example.com/$1 [L,NE,R=301]
RewriteRule ^products/([^/\.]+)?$ products/product-details.php?alias=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.*)/?$ $1.php [NC,L]
</IfModule>
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]
I'm trying to redirect any non-existing URL back to its main page.
Below is my .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
Add that:
RewriteRule ^([^.]+\.[^/.]{3,4})/.* $1 [R=301,L]
And no need to use more than one RewriteEngine on for all the .htaccess.
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]
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]