.htaccess URL rewriting from foo.php to /foo/ - .htaccess

How to rewrite URL from:
http://subdomain.domain.com/dir/foo.php
to
http://subdomain.domain.com/dir/foo/
Using htaccess

This block of code worked, thanks to George.
RewriteEngine on
RewriteRule ^([^.?]+)$ filename.php [L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

paste this code to your .htaccess, file residing in the dir directory:
RewriteEngine on
# Rewrite filename.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
# Return 404 if original request is filename.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
it does redirect any .php extension, and if you type url with full .php extension it will show 404

Related

.htaccess redirect php to html and also I want add language flag except to English

Rules above is to replace .php to .html. Recently I added a another language for the site.
The site now running Chinese language when /?lang=hi exists in the browser address bar.
It's kinda ugly from the example below:
- http://www.example.com/?lang=hi
- http://www.example.com/about-us.html?lang=hi
- http://www.example.com/contact-us.html?lang=hi
Example
What I want is Index of Hindhi
- http://www.example.com/?lang=hi equals to http://www.example.com/hi
- http://www.example.com/about-us.html?lang=hi equals to http://www.example.com/hi/about-us.html
- http://www.example.com/contact-us.html?lang=hi equals to http://www.example.com/hi/contact-us.html
http://www.example.com/ will not redirect to http://www.example.com/en
- English site will remain as http://www.example.com
- http://www.example.com/ equals to http://www.example.com/
- http://www.example.com/?lang=en equals to http://www.example.com/
RewriteCond %{REQUEST_FILENAME} =-f
RewriteRule ^(.*)\.php$ $1.html [NC,L]
RewriteRule ^(.*)\.html$ $1.php [NC,L]
RewriteRule ^(.*).html$ $1.php? [QSA]
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [L,R=301]
You can use these rules :
RewriteEngine on
##############################
#Redirect php to html
#This will redirect and rewrite your php files to html
#redirect file.php to file.html
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule .+ /%1.html [L,R,NE]
#Rewrite file.html to file.php
RewriteCond %{REQUEST_FILENAME} ^(.+)\.html$
RewriteCond %1.php -f
RewriteRule ^(.+)\.html$ /$1.php [L]
#############################
#language redirection
#1) /?lang=hi as /hi (for hompage)
RewriteRule ^hi/?$ /?lang=hi [L,NC]
#for html pages
RewriteRule ^hi/(.+)\.html$ /$1.html?lang=hi [L,NC]
Make sure to clear your browser cache before testing these rules.
When you are happy with the rules Change R to R=301 to make the redirection permanent and browser cached.

htaccess - only rewrite if "/" (Slash) contained

I am having some issues with Apache rewriting rules.
On my webserver the following files with a leading "s" exist (exerpt):
search.php
stats.php
status.php
Now, i want to rewrite search.php to simply s.
But when i now call status.php, my search.php is being included.
RewriteEngine on
RewriteRule ^stats$ stats.php
RewriteRule ^status$ status.php
RewriteRule ^s search.php # <---
RewriteRule ^live$ live.php
RewriteRule ^about$ about.php
RewriteRule ^i$ item.php
RewriteRule ^compare$ compare.php
DirectoryIndex home.php
How can i force Apache to only rewrite search.php to s if the URL is like
http://localhost/s/PARAM1/...
I tryed RewriteRule ^s/ search.php without success.
EDIT: 404 Error at .../s/PARAM1/..., stats, etc. works.
I hope you'll understand my issue although I'm not very familiar with the rewriting rules.
Reorder your rules and make use of regex to reduce your rules like this:
DirectoryIndex home.php
RewriteEngine on
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(statu?s|live|about|compare)$ $1.php [L,NC]
RewriteRule ^i$ item.php [L,NC]
RewriteRule ^s/ search.php [L,NC]

Wordpress Multisite .htaccess .. please explain the functionality

What is this .htaccess file doing??? I don't know anything about .htaccess .please help.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
RewriteEngine On
This turns on the rewrite engine, all the rules get ignored if this isn't in the htaccess file
RewriteBase /
This tells the rewrite engine that relative URL-paths should use this as its base
RewriteRule ^index\.php$ - [L]
This rule means: "If the request is index.php, then pass it through the rewrite engine and stop rewriting"
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
This rule matches against <some stuff> then wp-admin, and redirects the browser to the same URL except it ends with a slash. Example: http://example.com/foo/bar/wp-admin gets redirected to http://example.com/foo/bar/wp-admin/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
This rule says if the request is for a file or directory that exists, pass it through the rewrite engine and stop rewriting.
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
This rule matches against <some stuff> then wp-content or wp-admin or wp-includes, and rewrites the URI to everything after the <some stuff> Example: http://example.com/blah/blah/wp-admin/some/more gets rewritten to /wp-admin/some/more.
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
This rule matches against <some stuff> followed by any php file, and rewrites the URI to everything after the <some stuff>. Example: http://example.com/abc/123/somefile.php gets rewritten to /somefile.php.
RewriteRule . index.php [L]
Finally, if none of the other rules got applied, this rule rewrite everything else to /index.php.

.htaccess rewrite rule giving server error

I think I just need a second pair of eyes for this as I can't see why i'm getting a server error.
RewriteEngine On
RewriteRule ^Gig/([a-zA-Z0-9_-]+)$ gig.php
RewriteRule ^Gig/([a-zA-Z0-9_-]+)/$ gig.php
#allow non caps
RewriteRule ^gig/([a-zA-Z0-9_-]+)$ gig.php
RewriteRule ^gig/([a-zA-Z0-9_-]+)/$ gig.php
Edit:
I have now viewed the log and the reason is there are far too many internal redirects. I myself am not too competent at mod_rewrites etc so please have a look.
#redirect so home page shows /Home
Redirect /home.php http://localhost/Home
#add php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Redirect /home.php http://localhost/Home
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.*)\.php /$1 [R=301,L]
#redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.^([a-zA-Z0-9_-]+)$ [NC]
RewriteRule ^(.*)$ http://^([a-zA-Z0-9_-]+)$1 [L,R=301]
#remove trailing slash
RewriteEngine on
RewriteRule ^(.*)/$ /$1 [L,R=301]
#allow artistprofile nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ artist_profile.php
#info nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Aa]bout/?$ artist_about.php
#gigs nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Gg]igs/?$ artist_gigs.php
#tracks nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Tt]racks/?$ artist_tracks.php
#gig nice url
RewriteEngine On
RewriteRule ^[Gg]ig/([a-zA-Z0-9_\-]+)/?$ gig.php
That's all the rewrites in the .htaccess file
Edit:
The problem is the adding PHP extension part
#add php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I think its due to the ([a-zA-Z0-9_-]+) being after the file name.
You need to escape your - characters when they're inside brackets. You can also shorten your code to the following:
RewriteEngine On
RewriteRule ^[Gg]ig/([a-zA-Z0-9_\-]+)/?$ gig.php
# Escape it! ^^
Why can we shorten it like this?
[Gg] means "The character G or g"
/? means "/ repeated 0 or 1 time"

.htaccess mod_rewrite Convert file to php and don't show filetype

Given a set of URL as follows:
http://site.com/filename.html
http://site.com/filename.htm
http://site.com/filename.php
http://site.com/filename
Using the mod_rewrite module in .htaccess, how can I make it request the file at
http://site.com/filename.php
and show the URL
http://site.com/filename
This should satisfy your requirements:
RewriteRule ^([^\.]+)$ /$1.php [L]
EDIT: After your comment and changes in the question - try this:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\. /$1 [R,L]
RewriteRule ^([^\.]+)$ $1.php [L]
Try these rules:
# remove file name extension and redirect externally
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]+\.(html?|php)
RewriteRule ^([^.]+)\.(html?|php)$ /$1 [L,R=301]
# rewrite to PHP file internally
RewriteRule ^[^.]+$ $0.php [L]

Resources