Replace php extension with slash / using .htaccess - regex

I want to rewrite php extensions with .htaccess. I want to replace php extension with slash / . For example If I have: http://example.com/about-us.php will be http://example.com/about-us/
How can I make this modifying this piece of code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule .*[^/]$ $0/ [L,R=301]
Thanks to all!!!

You can use:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Tested with Apache 2.2 and Apache 1.3.7
RewriteEngine On
RewriteBase /
You can do it with one line
RewriteRule ^(.*)/$ /$1.php [L]
Apache 2.2
for http://www.domain.com/services.php
and get it with
http://www.domain.com/services OR http://www.domain.com/services/
Apache 1.3.7
if you want
http://www.domain.com/services/
you need ..//$
RewriteRule ^(.*)//$ /$1.php [L]
Update
http.conf
LoadModule rewrite_module modules/mod_rewrite.so
possible false options : must have MultiViews
DocumentRoot
<Directory "G:\Programme\Apache Group\Apache\htdocs">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>

In html head, include:
<base href="http://www.yourdomain.com/">
In htacces:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)\.[^.]+$ http://www.yourdomain.com/$1/ [L,R=301]
It's worked to me. Good luck!

Related

How to rewrite requests to index.php to parent directory

I have a website with two .htaccess files one in the projects root directory and one in the /Project Root/Public/ directory and I want to rewrite direct requests to "http://localhost/Project Root/index.php" to "http://localhost/Project Root/" how would I go about doing this?
My current .htaccess(s):
Project Root .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
/Project Root/Public/.htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} \s(?:/+(.+/))?public/ [NC]
RewriteRule ^ /%1 [L,R=301,NE]
DirectoryIndex index.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>
Have your Project Root .htaccess like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+([^/]+/)?index\.php[?\s] [NC]
RewriteRule ^ /%1 [L,R=301,NC,NE]
RewriteRule .* public/$0 [L]
Make sure you clear your browser cache before testing.

Error with remove any one or more empty parameters from anywhere in URLs code

This code is removing empty element or elements anywhere in URL but it turns my clean to dirty look URLs e.g. from http://www.example.com/US/FL/-show.html to http://www.example.com/search-products.php?productLocation=FL&countrySelect=US&submit=1
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.+?&)?[^=]+=(?:&(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=302,L,NE]
My clean URL setting via .php is:
<select name="country" id="country" onchange="document.forms[0].action='<?php
echo $submit == 1 ? '/search.php' : '/index.php' ; ?>';document.forms[0].submit();return false;">
Full .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]
RewriteCond %{QUERY_STRING} ^(.+?&)?[^=]+=(?:&(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC,OR]
RewriteCond %{REQUEST_FILENAME} -l [NC]
RewriteRule .* - [L]
RewriteRule (.*)/$ /?countrySelect=$1
RewriteRule (.*)/+/(.*)-(.*)\.html$ search-products.php?countrySelect=$1&productSelect=$2&submit=1
RewriteRule (.*)/tag/(.*) search-products.php?countrySelect=$1&keyword=$2
RewriteRule (.*)/(.*)/(.*)-(.*)\.html$ search-products.php?productLocation=$2&countrySelect=$1&productSelect=$3&submit=1
RewriteRule (.*)/(.*)_(.*)\.html$ detail.php?name=$2&id=$3&detail=true&countrySelect=$1
</IfModule>
# Protect the htaccess file
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
# Protect json
<Files ~ \.json$>
Order Allow,Deny
Deny from all
</Files>
# Disable directory browsing
Options All -Indexes
Try changing empty parameter removing rule to this:
RewriteCond %{THE_REQUEST} \?(.+?&)?[^=]+=(?:&(.*))?\sHTTP
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=302,NE,L]
And test in a new browser.

htaccess - redirect removing part of url

I would like to remove ALL the -del and .html like this
example:
http://website.com/test-del/test2-del/test3-del.html
http://website.com/test/test2/test3
Thanks in advance.
My curreny httaccess file
Options +FollowSymlinks
# Prevent Directoy listing
Options -Indexes
# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Order deny,allow
Deny from all
</FilesMatch>
# SEO URL Settings
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^system/download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymlinks -Indexes
# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Order deny,allow
Deny from all
</FilesMatch>
# SEO URL Settings
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteRule ^(.+?)-del(?:(/.*)?|\.html)$ /$1$2 [L,NC,R=302]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^system/download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

Can .htaccess urls be redirected like this?

I have my .htaccess set up and it is working great for links like:
http://localhost:8888/z-jquery/view
http://localhost:8888/z-jquery/search
so it is removing .php extension and also redirecting if they are typed in manually which is perfect but I also need my other urls like:
http://localhost:8888/z-jquery/edit?client=dane+kasbo
to be like this if possible:
http://localhost:8888/z-jquery/dane+kasbo
this is my .htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /z-jquery/
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Note: I have searched a lot of .htaccess questions here but none work for me, any help would be much appreciated.
You can use:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /z-jquery/
RewriteCond %{THE_REQUEST} /edit(?:\.php)?\?client=([^&]+)\s [NC]
RewriteRule ^ %1? [R,L,NE]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NE]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^([^/.]+)/?$ edit.php?client=$1 [L,QSA]

Htaccess Removing .php extension to .html make an URL Not found

To remove .php extension from URL and replaced with .html
i have wrote:
Options -Indexes
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.php $1.html [QSA,L]
This work fine and i the extension changed, but i get this message:
Not Found
The requested URL /**xxxxxx**.html was not found on this server.
To serve .php files as .html you can use this code:
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1.html [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)\.html$ $1.php [L,NC]
wrong way round? try
RewriteRule ^(.*)\.html $1.php [QSA,L]