I would like to delete extensions like .php, .html, etc from my url and this is what I use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
It works but I don't know if this is the best way to do this and I also don't understand how it works, could someone explain it?
The second thing I would like to realise is to make the URL more beautiful by doing something like this:
this is my URL: http://domain.com/portfolio/project.php?id=1
this is what I would like to see: http://domain.com/portfolio/project/1
Thanks in advance
You can use these rules in site root .htaccess:
RewriteEngine On
# rewrite /portfolio/project/25 to /portfolio/project.php?id=25
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(portfolio/[\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA,NC]
# rewrite /portfolio/project to /portfolio/project.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# rewrite /portfolio/file to /portfolio/file.html
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Related
I need help with my htaccess file. I added the following to remove all .php extensions which is working fine.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
However I have a directory /users which I want to rewrite the URL strings. The current URL is like this:
aero.site/maki/users/index.php?t=mf5cc
I want the URL to look like
aero.site/maki/mf5cc
I used the following rewrite rule but it's giving me Object not found error:
RewriteRule ^([^/.]+)$ users/index.php?t=$1 [L]
My complete htaccess content is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^/.]+)$ users/index.php?t=$1 [L]
Kindly help me with the correct rewrite rule for the last line to turn aero.site/maki/users/index.php?t=mf5cc into aero.site/maki/mf5cc
FYI: my htaccess file is in aero.site/maki directory
I finally got it to work. Thanks to #misorude. Here's my updated .htaccess content:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^/.]+)$ users/index?t=$1 [L]
My site has links that ent to .html or .php.
I would like to have clean urls without trailing slash at the end.
With
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
I have clean urls but with trailing slash at the end. Whatever I tried had no success..
Thank you!
To remove a trailing slash use:
RewriteEngine on
RewriteRule (.+)/$ /example/$1 [L,R=301]
Options is required by Many Hosting
Options +MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
If you need to add any other file for instance .txt just add this
RewriteRule ^([^\.]+)$ $1.txt [NC,L]
Thank you for replies!
This works perfectly:
Options +MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Thanks a lot!
I am trying to find a better solution to solve the issue. I am not using any framework like silex, Yii, etc. Thus I do not have the general routing handling mechanism provided by these frameworks. My URL patterns are like
routing.php
routing.php?action=create
routing.php?action=list&id=1
and the resulting URL I want to have are
/routing
/routing/create
/routing/list/1
I have some very basic understanding of .htaccess, below is my current foundings
RewriteEngine On
RewriteBase /name/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
These codes work well to put any .php to /something. However, I am stuck to continue to generate a general solution for getting /routing/create and /routing/list/1 . I appreciate for any helps.
Have your .htaccess like this:
RewriteEngine On
RewriteBase /name/
RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ $.php1?action=$2 [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?action=$2&id=$3 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
My current .htaccess file contains the following lines:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I would like to redirect www.domain.com/category/automotive/used+car+dealers to www.domain.com/category.php?category=automotive&subcategory=used+car+dealers.
Should be dynamic and not a static redirect, as I will have other categories and subcategories? Can anyone show me some light on how to proceed from here?
I have googled, and browsed Stack Overflow for nearly 2 hours, but I'm still unable to come out with a solution.
[Sample 1]
RewriteEngine On
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have tweak abit and is able to redirect to the correct file, but somehow the page loads without css and images. please advise. thankyou.
[Sample 2]
RewriteEngine On
RewriteBase /websitefolder/
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This works fine just that the url will become category.php?category=automotive&subcategory=used+car+dealers instead of /category/automotive/used+car+dealers. but i wish to have the latter. please advise
Try this:
RewriteEngine On
RewriteBase /
RewriteRule ^(category)/([\w-+]+)$ $1.php?$1=$2 [R,L]
RewriteRule ^(category)/([\w-+]+)/([\w-+]+)$ $1.php?$1=$2&subcategory=$3 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine On
RewriteBase /websitefolder/
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Just remove the R flag from :
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [R,L]
It will not change the URL in the browser.
Here's what should fit your exactly your need:
RewriteEngine On
RewriteBase /
RewriteRule ^category/([a-zA-Z0-9]+)(/([a-zA-Z0-9]+))?$ category.php?category=$1&subcategory=$3 [QSA,NC,L]
Now if you want something more generic, i.e.:
www.domain.com/category/automotive/used+car+dealers
=>
www.domain.com/category.php?category=automotive&subcategory=used+car+dealers
and
www.domain.com/title/automotive/used+car+dealers
=>
www.domain.com/title.php?title=automotive&subcategory=used+car+dealers
Please see ThinkingMonkey's answer it's better than mine :).
If I have a url http://example.com/page.php
but I want the page to display when someone goes to http://example.com/page/
how do I go about doing that?
Try this:
RewriteEngine on
RewriteRule ^([^/]+)/$ $1.php
I usually use something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule ^$ index.php [L,QSA]
RewriteRule ^([^/\.]+)/?$ $1.php [L,QSA]
</IfModule>
Note the question mark after the slash. You could add that to Gumbo's example to "test" for a trailing slash (so it can be there or not).