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]
Related
Instead of
http://example.com/file.php?color=red
I would like to use
http://example.com/file/red
I have removed my file extensions by adding the code below to .htaccess which will display the content of the http://example.com/file.php on the server when accessing http://example.com/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 [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]
Now I am looking for a way to pass the "red" parameter to the file when putting this address in the web browser like this http://example.com/file/red
Thanks!
EDIT:
I got the answer to look for something called friendly URLs and that helped me.
I ended up with replacing the first code with this.
Options -MultiViews
RewriteEngine On
# redirect "/file.php?id=xxx" to "/file/xxx"
RewriteCond %{THE_REQUEST} \s/file\.php\?color=([A-Za-z0-9_]+)\s [NC]
RewriteRule ^ /file/%1? [R=301,L]
# internally rewrite "/file/xxx" to "/file.php?id=xxx"
RewriteRule ^file/([A-Za-z0-9_]+)$ file.php?color=$1 [L]
Then with PHP I will just request the parameter like this.
$color = $_GET['color'];
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]
can anyone help me to remove .html or .php extension in URL.
I am trying this below code in my php project, but it is not working.
I have tried so many .htaccess codes, but nothing helps me..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.php
Thanks in advance
Previously I also faced problem with removing .html or .php extension form URL.
I have a Solution Here
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]
put the above code in .htaccess file, above code is for .php files.
for .html files go through the below code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
I just replaced the .php with .html
Hope it helps You.. :)
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 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.