Remove .html from url - php

I want to remove .html from all url's of site, I am working on x-cart and want to make it possible using .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
</IfModule>
I used this code but it doesn't work

use like:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)/?$ $1.html [NC,L]

You can use:
# remove html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]
# add html but only if file exist with html and not without
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]

you can use this to hide .html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]

Related

How to change more extensions in .htaccess with mod_rewrite?

Now I have this and rewrite index.php to index
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.])$ $1.php [NC,L]
But I want also that the file names about.html change to about without the .html
How do i do that?
See This
FOR REMOVE .PHP
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
FOR REMOVE .HTML
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

How to remove .php extension using htaccess

I have .htaccess file and inside it I have this code:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule (.*) /cvsrrc/index.php [L]
</IfModule>
I tried to add this which removes the .php extension but, I got 404'd.
RewriteRule ^(.*)$ $1.php
What I want to achieve is to add some code on my htaccess to remove the .php extension without removing the existing code above because I used that for my angular route. Thanks!
Try this. Got this from my project:
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
you can try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
edit:
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]
As #starkeen says, just add the following line into my .htaccess code. And here it is :
Options +Multiviews
This one works for me
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

user friendly url without filename in core php site

I have a link www.example.com/folder/filename.php?cid=1&title=Chekcing
I want to make user friendly url from above link to : www.example.com/folder/1/Checking/
I have an .htaccess code :
**
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?cid=([^&\s]+)\&title=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?cid=$2&title=$3[QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
**
But it only returns filename without .php : **www.example.com/folder/filename/1/Checking**
I want to remove filename too, from the url...
Thanks in Advance...
Try this :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteEngine On
RewriteBase /foldername/
#Remove .php extensions from php files :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
#__________
#Redirect urls with query strings to cleaner version :
RewriteCond %{THE_REQUEST} /filename\.php\?cid=([^&]+)&title=([^\s]+) [NC]
RewriteRule ^ %1/%2? [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ foldername/filename.php?cid=$1&title=$2 [QSA,L,NC]
#___________

rewrite dynamic url after form submit

My link is
search?c=category&s=product
I have already removed the .php with htaccess, everything is working apart from when a user searches, the first page will show the link above, I want to show the link as below:
search/category/product
I tried the below code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^product/(.*)/(.*)/(.*) product.php?id=$1&c=$2&name=$3
RewriteRule ^search/(.*)/(.*)/(.*) search.php?c=$1&s=$2&page=$3
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} \ /+search\?c=([^&\ ]+)&s=([^&]+)
RewriteRule ^ /search/%1/%2? [L,R]
What am I missing in the above?
Have rules this way:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /site/
RewriteCond %{THE_REQUEST} /search\?c=([^&\s]+)&s=([^&\s]+) [NC]
RewriteRule ^ search/%1/%2? [L,R]
RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)/?$ product.php?id=$1&c=$2&name=$3 [L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/?$ search.php?c=$1&s=$2&page=$3 [L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?c=$1&s=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Replacing question marks and equal signs with htaccess

I'm trying to replace question marks and equal signs so that I can use URLs like the following:
http://www.mydomain.com/categories/id/23/name/category-name
The above url would be internally redirected to
http://www.mydomain.com/categories?id=23&name=category-name
I am using this .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# external redirect from /view.php?id=1 to /view/id/1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [L,R=301]
# internal forward from /view/id/1 to /view.php?id=1
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L,QSA]
but it's not working as expected. When I go to http://www.mydomain.com/categories/id/23/name/category-name I get a 500 internal server error. Why? What am I doing wrong?
updated code
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$2 [L,QSA]
This rule should take care of recursively replacing each / with = and finally making it a nice Query String:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$2 [L,QSA]
You need to comment out or delete this rule:
# RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L,QSA]
UPDATE: You .htaccess need re-ordering and little bit of tweaking. This should work:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mydomain/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/(.+?)&?$ $1.php?$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Categories