I have a simple problem, i tried to remove .php on the address bar using this code below
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally redirect /dir/foo/ to /dir/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
and it works but eventually it keeps on displaying "OBJECT NOT FOUND" how do i fix this problem?
You may try to use this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Example: Your page is: www.yoursite.com/yourpage.php . Now you can access the same page without ".php".
ALSO
You can try to remove [R] permanent redirect (to remove your error), and mantain the extension in rule.
I hope this will help you!
If all you want to do is remove the .php extension from your URLs, just use Options +MultiViews, no need for the rewrites.
Related
I know am re requesting the question which are already asked before but i tried all the posts and did all the research but still am unable to over come the problem i tried to put the below second snip first to htaccess in www directory and as well as folder directory the link of the page later i added the small snip it to the htaccess also but still its not working very disappointed my hosting provider is Godaddy if i add .php it works
htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
second
Options -MultiViews
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.elmorfeo\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)index$ $1 [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
Following one works for me
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]
I found the answer that was all the above written ware correct but godaddy Cpanel there is no option to show hidden files i accessed it via ftp i found the original the correct htaccess file i did the changes there problem solved
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]
I'm so newbie in apache, so it's a basic question but I couldn't solve this using another questions or links.
I tried to change my URL using .htaccess and I had two purposes;
hide .php extension
change some queryString from somefile.php?id=bar to somefile/id/bar which bar is a number or mixed string like T51-3.
My queryString is http://localhost/payment/theme/ticket?id=770314 that I want change it to http://localhost/payment/theme/ticket/id/770314 or
http://localhost/payment/theme/ticket/770314
I found this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# 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 [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
in stackoverflow.com/this_question
This was working nice but didn't solve my second issue.
So, I searched for a while in this site and others and did find some sample codes like this one :
RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)$ ticket.php?id=$2
or
RewriteRule ^(.*)$ /ticket.php?url=$1 [L]
and etc...
but none of these didn't work for me.
Some of them will make the server done and some don't have any affect at all.
Would you please guide me to right way...
I looked at these questions:
htaccess rewrite for query string
how to change query string parameters with name using .htaccess?
How can I use .htaccess to hide .php URL extensions?
And these links:
http://simonecarletti.com/blog/2009/01/apache-query-string-redirects/
https://wiki.apache.org/httpd/RewriteQueryString
UPDATE :
This is my site strucure:
There is an htaccess for important folders, like, forms, db, classes etc...
All my site pages are in the theme folder, as you can see in the picture.
In the root htaccess, there's just two lines that auto start sessions.
And I added your code to theme's htaccess.
Thanks in Advance
You can use this code in
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /payment/theme/
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php\?(\w+)=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# to handle http://localhost/payment/theme/ticket/id/770314
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/([\w-]+)/([\w-]+)/?$ $1.php?$2=$3 [L,QSA]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
I'm using the below code to remove .html extention form url, but it is not working..
can any one help , where i have to put the .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
I would like to share an understandable, simple way to remove the .html extension from the URL using the .htaccess file. I'm using XAMPP on Windows 10. For me, it's working perfectly. If you want the same result, just follow the steps given below...
For removing the .html extension, e.g. if you want to change mywebsite.com/contact.html to mywebsite.com/contact you have to add the following code inside your .htaccess file:
First of all, create a new file named .htaccess and placed it at your root-directory where your index file is placed.
Now, open this (.htaccess) file with any editor of your choice and write/copy/paste the given code in your file and save it...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
Now, if you (any user) will access mywebsite.com/contact in the browser, it will show the content of mywebsite.com/contact.html page.
So, what if any user will access the URL as mywebsite.com/contact.html? this will redirect the user to this mywebsite.com/contact.html page instead of this mywebsite.com/contact which means the user can still visit the mywebsite.com/contact.html page. Don't worry about it. If you want to avoid this, the next step is for you...
To avoid the above problem and for redirecting users to the newly created URL, you need to add some more rules in your .htaccess file. So, open it again and replace your old code with the new one given below and save your file...(You can ignore this step, It's on you...)
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Now, you're all set.
I hope this will fix everyone's problem.
Have a nice day :)
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Finally I Got the correct .HTACCESS code to remove .php extention... :)
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 this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
Good days guys, I need your help with this url rewrite.
The are two rewrites I want to do.
1) I want to take out .php from the url even though my files is saved as .php
2) I want to rewrite the url below
If you can help me. It will be lovely. Thanks
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
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]