htaccess: Remove .php extension - .htaccess

i have a file named Show.php
i want to remove .php extension of this, and if someone requested /Show.php, redirect him to without .php extension Page.
This is my htaccess but it does not redirect user to without extension page.
RewriteCond %{REQUEST_URI} ^Show\.php$
RewriteRule ^Show\.php$ ./Show [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Show$ ./Show.php [L]

REQUEST_URI, unlike the RewriteRule expression, begins with a leading / but your expression begins with ^Show. Simply adding the leading slash should do the job. Everything else looks correct.
RewriteCond %{REQUEST_URI} ^/Show\.php$
#-------------------------^^^^^
RewriteRule ^Show\.php$ Show [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#Rewrite to Show.php rather than ./Show.php
RewriteRule ^Show$ Show.php [L]

Related

Double RewriteRule on .htaccess

I did a php blog and I would like to have friendly SEO urls for my posts, and also remove .php extension from my urls. So I edited my htaccess file like this :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [N]
#second condition and rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So I got my friendly SEO post urls but the second condition doesn't work. But it works when I use it as my first condition. What I did wrong ? Thanks a lot for your help
Keep first rule as .php handler rule but that should check for presence of .php file as below:
RewriteEngine On
# if not a directory and file.php exists then rewrite file to file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# rewrite every non-file or non-directory to viewpost.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ viewpost.php?id=$1 [L,QSA]

htaccess file extension hiding and pass url string

I am trying to hide .php extension and also rewriting the url string with trailing slashes. File extension hiding is working fine but url string is not.
From:
http://www.example.com/abc.php?id1=1&id2=2
To:
http://www.example.com/abc/id1/1/id2/2
Following is my .htaccess
RewriteEngine On
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\/([0-9]+)$ $1.php?id=$2&%1 [L]
First, check %{THE_REQUEST} variable. Redirect if it satisfies your match conditions.
RewriteCond %{THE_REQUEST} ^GET\ /abc\.php\?(id1)=(\d+)&(id2)=(\d+) [NC]
RewriteRule ^ /abc/%1/%2/%3/%4? [R=301,L]
Next, internally rewrite the friendly-url.
RewriteRule ^abc/(id1)/(\d+)/(id2)/(\d+)$ /abc.php?$1=$2&$3=$4 [NC,L]

htaccess - URL rewrite - Remove slashes, but not from files

I use PHP.
A working htaccess-file
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php/$1 [L]
Look at the row with a # comment. When uncommented it adds a redirect to a slash. I use URL rewrite with toroPHP.
I want to rewrite to ending slash
I want to redirect to ending slash from rewritten URLs, just like the code above.
I don't want ending slash from real files, like jquery.js, style.css.
Example (updated 2012-12-21)
/category/test should be /category/test/
http://www.test.com/myjsfile.js should be http://www.test.com/myjsfile.js
Problem
If I use the code above uncommented it add an ending slash to all urls, including javascript files and css files.
I only want the rewritten urls to end with slash.
Question
Can it be done with htaccess? If so how?
The htaccess was almost correct. However the REQUEST_FILENAME needs to be before EVERY rewrite rule, not just before the first.
This works
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]

htaccess physcially redirect in case of 404

To start off I know how to do a simple redirect to a 404 when a page doesn't exist with.
ErrorDocument 404 /index.php
Is what I want to do is a physical redirect to the index page. So if a user types in mydomain.com/abc and that page doesn't exist it does an actual redirect TO the index page. I don't want the url to remain the same with the bad address. Yes I know its not a big deal but it bugs me. I could accomplish this by having a custom 404 page redirect to the homepage but I don't want this.
I've tried this with the [R] redirect flag and obviously it doesn't work.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(.*)$ /index.php [L,R]
Update
This is the error I get it rewrites the url to mydomain.com/index.php
I tried to remove the index.php with just / and it didn't redirect but gave the same error below.
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept
cookies.
Do this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]+)/?$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,R]
Explanation:
checks whether the file has extension:
RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
If not, checks whether file is present:
RewriteCond %{REQUEST_FILENAME} !-f
If not a file, checks whether it is a folder which is present:
RewriteCond %{REQUEST_FILENAME} !-d
If not append .php. If / is present at the end, remove it and append .php
RewriteRule ([^/]+)/?$ $1.php
Checks whether the .php appended or whatever extension file is actually a file which is present:
RewriteCond %{REQUEST_FILENAME} !-f
check whether it is a directory:
RewriteCond %{REQUEST_FILENAME} !-d
If not, then it is a invalid file request and redirects it to /
RewriteRule ^(.*)$ / [L,R]
This should be your mod_rewrite based 404 handling:
# if it is not a file, directory or link
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L,R]
However I am not sure what are you doing in your first rule? Are you trying to add .php to each request URI?

apache mod-rewrite

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([0-9a-zA-Z_\-]+)(|\.html|\.php)$ page.php
this is the rule is .htacess.
what I want is if file is not found then redirect to page.php.
for example if url people.php ,people.html is not found then redirect to page.php.
because I also want those filenames without .php like people direct to people.php first. then check file exist or not, if not redirect to page.php.
how do I add one rule to make people redirect to people.php
These rules should work for you:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# if there is no . in request then append .php
# it is important to make [L] as last rule here
RewriteCond %{REQUEST_FILENAME} !\.
RewriteRule ^(.+)$ /$1.php [L]
# 404 handling for files that don't exist
# NC is for ignore case comparison
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^.]+\.(html|php)$ /page.php [L,NC]
See if this does what you're looking for:
RewriteEngine on
RewriteRule ^(?:.+/)?([^/.]+)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(?:html|php)$ page.php

Resources