Redirect .php to extensionless and make url without .php - apache

I dont really know too much about Rewrite in apache, but there is problem with my .htaccess file.
the original .htaccess is
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Unless directory, remove trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.domain.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.domain.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
ErrorDocument 404 /404.html
The first rewrite is for vanity url, another is redirecting non www to www and after that .php to extension less and redirecting .php file to extension less,
Problem i am facing is, if i request www.domain.com/ttt.php and if ttt doesn't exist then it redirects me to http://www.domain.com/profile?id=ttt.php
Also, i want the rewrite to be for php files not other type, Say if i have ttt.png file and i open www.domain.com/ttt it opens the image rather than showing 404 error.
Similarly happens with other type of files. It opens .txt file first if the names are common.
Thanks for any changes to this, seriously i don't want to mess my site.

This rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L]
Needs to be at the very end.
And this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Needs the [L] flag and needs to replace RewriteRule ^([^/.]+)$ $1.php [L]. Then you can duplicate this one and replace .php with .png, and .txt and every other extension you want to handle, in the order of preference:
Options +FollowSymLinks -Multiviews
RewriteEngine on
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Unless directory, remove trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.domain.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.domain.com/$1 [R=301,L]
# add extension if the php file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# add extension if the png file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png [L]
# add extension if the txt file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)$ $1.txt [L]
# add extension if the html file exists:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
# etc.
# finally, route to profile.php if all else fails
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]*)$ /profile.php?id=$1 [L]
ErrorDocument 404 /404.html
Since you're routing to profile.php, in the profile.php script, you'll need to redirect to /404.html if the id param doesn't exist (meaning the URI that got routed needs to be a 404).

Related

htaccess - remove index.php and keep a variable without key

original url is one of the following:
example.com
example.com/index.php
example.com/index.php?c=lorem
if example.com - nothing should be done
if example.com/index.php - it should be visible as example.com and here is my code - works fine:
RewriteEngine ON
RewriteRule ^index\.php/?$ https://%{HTTP_HOST}/ [NC,R=301]
RewriteRule ^/?$ index.php [L]
if example.com/index.php?c=lorem it should be visible as example.com/lorem
and lorem should be accessible by php in both cases:
if a user type example.com/index.php?c=lorem or
a user type example.com/lorem
pls help
Could you please try following, based on your shown samples.
Please do clear your browser cache before testing your URLs.
RewriteEngine ON
##This rule is for handling index.php file in uri.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php/?$ / [R=301,NC,L]
##This rule is to redirect from index.php?c=lorem to lorem in uri.
RewriteCond %{THE_REQUEST} index\.php\?c=(lorem)\s [NC]
RewriteRule ^ http://%{HTTP_HOST}/%1? [R=301,L,NE]
##Rule for non-existing files/directories to index.php with variables.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?c=$1 [L]
OR: Above is for specifically lorem in case your query string could be anything then try following.
RewriteEngine ON
##This rule is for handling index.php file in uri.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php/?$ / [R=301,NC,L]
##This rule is to redirect from index.php?c=lorem to lorem in uri.
RewriteCond %{THE_REQUEST} index\.php\?c=([^\s&]*) [NC]
RewriteRule ^ http://%{HTTP_HOST}/%1? [R=301,L,NE]
##Rule for non-existing files/directories to index.php with variables.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?c=$1 [L]
You may use these rules in your site root .htaccess:
DirectoryIndex index.php
RewriteEngine On
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php\s [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+index\.php\?c=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?c=$1 [L,QSA]

How can I use .htaccess to hide extension and prevent user access with extension url?

here is my code for hide .html and .php extension when user view my site, http://example.com/xxx.html to http://example.com/xxx
But I hope to make user when they input http://example.com/xxx.html, they cannnot access and jump to 404 not found.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html
# End of Apache Rewrite Rules
</IfModule>
Anyone have solution?
You can add this as your first rule:
RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC]
RewriteRule ^ - [R=404,L]
Update
Perhaps this will fix your ErrorDocument problem:
RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC]
RewriteCond %{REQUEST_URI} !=/error404.html
RewriteRule ^ - [R=404,L]

.htaccess on GoDaddy not working

I have web hosting in GoDaddy.
Call it: http://example.com
I have this below .htaccess
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
What I want is, when I open http://example.com/about.php will be http://example.com/about <-- without .php extension.
I tried to upload it to the hosting, but it always show error the file not found.
*Are we need to wait for long time to godaddy update the .htaccess?
To remove .php from pages:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L,NC,QSA]
and then, internal redirection to php pages:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
RewriteRule ^.*$ $0.php [QSA,L]

force remove .php extension in .htaccess

i know there are many questions like "How do i remove the .php extension, so that /test/ will be /test.php"
but if the user directly goes to test.php it doesnt replace the extension.
So I want to replace the .php it should be /.
here is the part of the .htacces I'm using:
RewriteEngine on
RewriteRule stuff\.php /other_stuff/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
You need to do the check against the actual request instead of the URI (which gets rewritten by other rules). The %{THE_REQUEST} variable doesn't change in the course of the rewrite engine. Try adding these rules right below the RewriteEngine directive:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^/]+)\.php(\?|\ |$)
RewriteRule ^ /%2/ [L,R=301]

htaccess to change url

I have the following code in my .htacess but it didn't work right. Is it because mod-rewrite is no "on", if so, how can i check?
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\$ $1.php [nc]
I wanted to rename my address, example:
http://www.abc.com -> http://www.abc.com
http://abc.com -> http://www.abc.com
http://www.abc.com/123.html -> http://www.abc.com/123
http://www.abc.com/12-12-12.html -> http://www.abc.com/12-12-12
http://subdomain.abc.com/123.html -> http://subdomain.abc.com/123
Basically removing the extension and ensuring that its www is intact.
Edited:
It was rewrote into
Options +FollowSymlinks
RewriteEngine on
RewriteCond % ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.*).php $1
but still not working
Step 1.
Change all of your links to have the .html removed from links. Make sure you have Multiviews turned off:
Options -Multiviews
Step 2.
You need rules to redirect the browser when there is a www missing from the hostname:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Step 3.
You need rules to redirect the browser when a URL is requested with .html or .php with the extensions removed:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]
Step 4.
You need rules to internally rewrite the URI if the request was actually for a php or html file:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
.htaccess
So all in all, you should have these rules in your htaccess (ditch any rules you may already have trying to solve this issue):
# Make sure Multiviews is off
Options -Multiviews
# turn on rewrite engine
RewriteEngine On
# Redirect requests that are missing www and not a subdomain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# Redirect if requests is for a .htm, .html, or .php
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]
# rewrite to the proper extension if such a file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
So altogether:
if browser URL address bar says: http://example.com/index.html it will be redirected to http://www.example.com/index
if browser URL address bar says: http://www.example.com/index it will be shown the content at http://www.example.com/index.html (or index.php if that exists). Address bar remains unchanged.
if browser URL address bar says: http://foo.example.com/images/image.png you will see the image at /images/image.png. Address bar remains unchanged.
if browser URL address bar says: http://foo.example.com/path/foo/bar.php it will be redirected to http://foo.example.com/path/foo/bar
if browser URL address bar says: http://foo.example.com/path/foo/bar it will be shown the content at http://foo.example.com/path/foo/bar.php. Address bar remains unchanged.
Remove .html:
RewriteRule ^(.*).html $1 [L,QSA]
Add www.:
RewriteCond % ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
On which hosting is located your website ? Are you sure mod-rewrite is enabled ? Did you contact your host to put it on, if not activated ?
Solution previously posted by androbin (https://stackoverflow.com/a/12553613/1713771) is correct.
The .htaccess file that I use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
</IfModule>
Which removes the .html file extension and forces www.