I'm in the process of tweaking our company website and can't seem to find the right .htaccess code to remove .html extensions from our pages (I'm not a coder, I am learning html on my own). I'm using this code to remove .html extensions and other things as well (other things work):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#removing trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#www to non
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
#html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://example.com/ [R=301,L]
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
Can anyone help? This is getting really frustrating as I've tried more than 5 different variations, all of which don't work!
Thank you.
to remove .html extension use :
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
read here for more
Related
I know there's a bajillian number of questions with this title and I've tried them all. What I'm trying to do is redirect
localhost/site/tours/picnics/
to
localhost/site/tours/picnics
but every code I've tried, (this one for instance)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
redirects me to localhost/picnics for some reason. I don't understand why it's happening. I've even tried RewriteBase /site/ but it didn't make any difference. Can someone point out the problem in this code?
Edit: Here's my complete htaccess file
RewriteEngine On
#RewriteBase /site/
RewriteRule ^destinations/(.*)$ destinations.php?destId=$1 [NC,L]
#RewriteRule ^tours/? tour-listing.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteRule ^tours/?(.*)$ tour-listing.php?cat=$1 [NC,L]
RewriteRule ^tour/([0-9a-zA-Z]+)$ tour.php?id=$1 [NC,L]
RewriteRule ^hotel/([0-9a-zA-Z]+)$ hotel-details.php?id=$1 [NC,L]
Change your rule to this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+(.+?)/+[?\s]
RewriteRule /$ /%1 [R=301,L,NE]
$1 captures value relative to your current directory only but %1 is being captured from %{THE_REQUEST} that has original and full REQUEST URL.
I have a redirect issue.
I am trying to redirect from http://www.project.com/index.php to http://www.project.com
I tried the following, but it goes one step back to any index.php like that.
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
To remove index.php use:
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
However, you can also use the below as an alternative (but it will require you to enable mod_rewrite for it to work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
In my hosting account, I have domains. One is located at the root and the other is in the /example/ folder. Here is the redirect code in my main .htaccess file, which works fine apart from the function I just described. This function was working previously, and has mysteriously stopped-
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/example(.*)$ http://example.com/$1 [R=301,L]
# Rewrite /example to http://example.com
# Remove .php from file names and force added slash
# http://stackoverflow.com/questions/1068595/htaccess-code-to-remove-extension-and-addforce-trailing-slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
Any ideas?
Try removing the leading slash, like this:
RewriteRule ^example(.*)$ http://example.com/$1 [R=301,L]
I tried to make my own .htaccess file with a few thing in it, but I'a not very familiar with it. I googled a lot but at the end I only managed to mess things up.
So, can someone give me working .htaccess code snippet?
I want it to do the following:
redirect from non-www to www
redirect index.php to / in all directories
remove ".php" from all files in all directories
redirect URL's like product.php?detail=Phone to product/detail/Phone
(also manage the trailing slash problem)
Hope someone can help.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#non-www to www
RewriteCond %{HTTP_HOST} ^nevca.getfreehosting.co.uk [NC]
RewriteRule ^(.*)$ http://example.co.uk/$1 [R=301,L]
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)index\.php$ /$1 [R=301,L]
#index/menu/123/ to index.php?menu=123
RewriteRule ^index/([^/]+)/?$ /index.php?menu=$1 [L]
#index.php?menu=123 to index/menu/123/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index2\.php\?menu=([^&]+)\ HTTP/
RewriteRule ^index\.php$ http://example.co.uk/index/%1/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Something like this, just the last 2 rules don't work...
And I also want the .php to be removed only when request is not like ".php?..."
There are some of these thing in the html5 boilerplate htaccess, you should take a look and use the part you want:
http://html5boilerplate.com/
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]