.htaccess clean urls without trailing slashes? - .htaccess

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!

Related

Rewrited URL giving 404 error

I'm using mode rewrite to make seo friendly urls. Please see the code below
Options +FollowSymLinks
RewriteEngine on
RewriteRule discover/([0-9]+)$ discover.php?page=$1 [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Code work in my local host but in the hosting server it doesn't work.
If i added with the file extension it works in the server but without the extension it doesn't work.
Ex: below works
RewriteRule discover-([0-9]+)\.html$ /discover.php?page=$1 [L]
Any pointers on how to make the 1st code it work? Appreciate your help.
Try this too,
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^discover/([0-9]+)$ discover.php?page=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Turn off MultiViews option and check for existence of .php file before adding .php in a request:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule discover/([0-9]+)/?$ discover.php?page=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Modify url with .htaccess file

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]

What is the simplest way to remove .html with an htaccess file?

So I have a been trying a lot of different htaccess codings to clean up my url
but am not even able to simply remove .html without something going wrong
Code Im using:
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
CODE Im hoping to clean up:
<area shape="poly" coords="3,612,78,557,118,627,5,708" href="collabwitus.html" />
Little new to htaccess and would love some help!
I am doing everything from file manager via hostgator so framing an answer in a more step by step version using file manager would be great!
Put the following code :
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.html[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This code worked for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Htaccess redirect a specific subdirectory

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 :).

htaccess works but content doesn't load

When I use index.php?id=this-is-an-article the page loads with the content, when I use /articles/this-is-an-article the page loads without any content, any idea as to how I can resolve this?
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# force www. in all requests
RewriteCond %{HTTP_HOST} ^mysite\.net [NC]
RewriteRule ^(.*)$ http://www.mysite.net/$1 [L,R=301]
# enable hiding php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)\$ $1.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^page/(\d+)*$ ./index.php?p=$1
# index.php?id=this-is-an-article => /articles/this-is-an-article
RewriteRule /articles/(.*) index.php?id=$1 [NC,L]
Thanks in advance.
Get rid of the leading slash. It's also best practice to begin a regex like that with ^ and end it with $.
RewriteRule ^articles/(.*)$ index.php?id=$1 [NC,L]

Resources