htaccess doesn't removing php extensions - php

I have a php project running. I am running my project on localhost. I want to remove php extensions from url in addressbar. I try several htaccess scripts but none worked some shows 404 not found error or doesn't work at all.
directory where my php project is D:\VertrigoServ\www\gotmation
I am using this htaccess script
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
# RewriteRule ^ %1 [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/gta/$1.php -f
# RewriteRule ^(.+?)/?$ /gta/$1.php [L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I am newbie php programmer doesn't have much experience of this stuff. Any help will be appreciated.

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This must work... you might have any other issues... do you have permitted .htaccess in apache config file???

try:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
These rules need to be in your document root.

Related

Access PHP files without typing extension but also allow folders default to index.php

I have the following .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To my limited understanding of Apache htaccess rules this is doing two things:
Redirecting http:// to https://
Allow access of domain.com/file.php files as domain.com/file
The problem is that rule #2 thinks every call to anything /name is a name.php when sometimes is an actual folder, and I need it to redirect to that folder's index.php file by default.
Any ideas?
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=307,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_FILENAME}.html [L]
## Redirect to HTTPS. Remove if you do not want this.
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
## Disables indexing (Always a good idea :D)
Options -Indexes
Remove the last line and try replacing with:
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
You need to check if %{REQUEST_FILENAME } exists as an existing php file before rewriting it:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https: //%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

htaccess not working in godaddy

i have host my website in godaddy webserver and i try to use htaccess file but it didin't work please tell me what the solution and help me to make it work
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## 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]
Use FileZilla or any other software navigate to the root of the website you want to apply
these changes.
Find .htaccess file (Use any editor to edit the file)
Change it with the following code.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^YOURSITE\.com\\index$ [OR]
RewriteCond %{HTTP_HOST} ^www\.YOURSITE\.com\\index$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "http\:\/\/www\.YOURSITE\.com\/" [R=301,L]
# Turn mod_rewrite on
Options +FollowSymLinks -MultiViews
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
# Redirect /product?pid=2 to /product/2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+product\?pid=([^&\s]+) [NC]
RewriteRule ^ /product/%1? [R=302,L]

write .htaccess rewrite website URL multiple parameters

I´m new to .htaccess and I am having problems in rewriting some URLs.
My website is made with php and has 2 parameters: param1 and param2.
The URLs look like similar to:
www.website.com/index.php?param1=12345678
www.website.com/index.php?param1=09876543
www.website.com/index.php?param2=abcdefgh
www.website.com/index.php?param2=qwertzui
I would like to create a .htaccess file to remove “index.php”, replace param1 and param2 with 2 names and add “.html” at the end, so they became:
www.website.com/budget/12345678.html
www.website.com/budget/09876543.html
www.website.com/user/abcdefgh.html
www.website.com/user/qwertzui.html
I have got this code (copied from internet).
It removes the .php extension but in the internally forward it rewrite it at the end of the URL neglecting the parameters.
Is someone so kind to help me to write the code?
Thanks :)
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
You can try:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# handle ?param1=...
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?param1=([^\s&]+) [NC]
RewriteRule ^ /budget/%1.html? [R=301,L,NE]
# handle ?param2=...
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?param2=([^\s&]+) [NC]
RewriteRule ^ /user/%1.html? [R=301,L,NE]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,NE,L]
RewriteRule ^budget/([^/.]+)/?$ index.php?param1=$1 [L,QSA,NC]
RewriteRule ^user/([^/.]+)/?$ index.php?param2=$1 [L,QSA,NC]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

How to remove .php extension from live website url

I referred alexcican website and put the code in my htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
It works in my localhost, but doesn't work in thesvn link or live website. Please help.
Are you using apache server? Check if you have mod_rewrite enabled.
Just in case, if you have created any folder on you live website for your code, than try using below line
RewriteBase /yourFolderName/
keep above code just below RewriteEngine On
This one worked for me:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

mod_rewrite rule 500 internal server error

I want to redirect from extension URL to extensionless one. For example, /contact should read the contents of /contact.php. I use the following rules to achieve this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
There is a file called test.php that has a variable called "id". I want to redirect read requests from /test/1 to test.php?id=1
I use the following to achieve this:
RewriteRule ^test/([0-9]+)/?$ /test?id=$1
For some reason calling the test/1 file leads to a 500 internal server error. If I remove the rules to hide the php extension, it works again
Also how can I force a permanent redirect on the URL with extension? For example if someone ties to reach /contact.php should be redirected to /contact
Thanks
You can use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# your existing rule (fixed)
RewriteRule ^test/([0-9]+)/?$ /test.php?id=$1 [L,NC,QSA]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L]
This is in my htaccess ... Be sure to have mod_rewrite turned on in Apache...
RewriteEngine On
# Redirect .php requests to url without an extension
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
RewriteRule ^([^/.]+)$ $1.php [L]
# This will remove the trailing slash unless it's a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

Categories