how to hide .php extension from url? - .htaccess

I am New in Php Development. Any error in the following code..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Its not working in my web site.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
and put your htaccess file in the parent directory.

Related

How can I have 2 rewrite rules on my .htaccess file

I currently have an .htaccess file that has one rewrite rule that removes .html however I cannot seem to be able to do the same thing with .php without messing up my whole website
This is the code that I have in my .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
Thanks
Sure you can, why not?
Your version:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Some lightly modified version:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^ %{REQUEST_URI}.html [END]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^ %{REQUEST_URI}.php [END]
You generally should switch off MultiViews when implementing such rules by the way.

multiple rules of URL rewriting

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html[L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)$ /profile.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ /profile.php?name=$1
This is currently .htaccess of my website. The rewriting supposes to have two functions, makes the internal url example/profile.php/name=xyz into example/xyz as external url, and also removes the extensions of html files.
But the extension removing is not working, and such as for a file named xyz.html, it gets 404 and
example.com/xyz/?name=xyz
At the address bar. I think there is some conflicts between two rules.
RewriteCond is only applicable to very next RewriteRule hence your last rule is running without any RewriteCond. Also you need to use [L] (Last) flag in all of your rules.
Use this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ /profile.php?name=$1 [L]

Set language using htaccess URLrewrite

I have for example two files in my root directory: one.php and two.php. I would like to reach them through these urls without having to actually have the physical directories en+de.
mydomain.com/en/one
mydomain.com/en/two
mydomain.com/de/one
mydomain.com/de/two
So the first directory would be "ignored" and just pass a GET variable with the language setting to the php-file, second directory is the php-file without extension.
This is what I'm using in my htaccess to loose the file extension so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
UPDATE/SOLUTION
This is quite useful: .htaccess rule for language detection
and this seems to pretty much do what I wanted:
RewriteRule ^(en|de|fr)/(.*)$ $2?lang=$1 [L,QSA]
RewriteRule ^(.*)$ $1?lang=en [L,QSA]
ONE MORE ADDITIONAL QUESTION
For mydomain.com/en/one/id45
If I wanna pass id45 as a variable to one.php what line in htaccess do I have to add?
You can try this rule:
DirectoryIndex index.php index.html
DirectorySlash On
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$2.php -f
RewriteRule ^(en|de|fr)/([^/]+)(?:/(.*)|)$ $2.php?lang=$1&var=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/?$ index.php?lang=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1?lang=en [L,QSA]

htaccess remove html and php duplicated is not working

this snippet works fine for removing php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
However, if I add html condition, it will not work
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Did I do something wrong?
You may try this instead, in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !\.php [NC]
RewriteRule .* %{REQUEST_URI}.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !\.html [NC]
RewriteRule .* %{REQUEST_URI}.html [NC,L]
You should be aware the implementation in your code works only when the request doesn't have a trailing slash. I just modified your code to work, but I think it's worth nothing that problem. For example:
For a request like http://example.com/filename/, the file to match with condition RewriteCond %{REQUEST_FILENAME}.php -f is filename/.php. That file can't exist, therefore, the respective rule will be never used.
Option to remove the trailing slash when present:
Add these lines after RewriteEngine On in the previous code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*[^/]+)/$ [NC]
RewriteRule .* /%1 [NC,L]

folder doesn't load while making the URLs work with and without the .php

I included the code for making the URLs work with and without .php. I included the below written code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now the url works with and without .php. For e.g. if we put http://www.test.com/test.php or http://www.test.com/test both works. But the problem is folders doesn't load. For e.g. http://www.test.com/admin doesn't load. not found error is shown. Admin is a folder. Any idea?
Try this code for .php extension removal instead:
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [R=301,L,NC]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_URI}.php [L]
Make sure to comment out your existing code
Try below :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [NC,L]
this will also work with a directory path.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
just try this...I think its working

Resources