Im trying to fix this warning:
Avoid use of file extensions wherever possible.... Consider URL rewriting as an effective and transparent means of creating appropriate URLs
I added to my .htaccess file this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Its working fine on example.com/page but Im getting an Internal Server Error on example.com/page/
Any ideas?
Change your regex to allow an optional trailing slash:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1.html [L]
Related
I have a problem with hide URL in .htaccess. I want to remove .php from the URL.
For Example: convert www.example.com/destination_info.php/Laos-destination-trip to www.example.com/destination_info/Laos-destination-trip
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/(.+)$ $1.php/$2 [L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/(.+)$ $1.php/$2 [L]
To hide .php use following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To hide html
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Try this, To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A reference to the full article can be found here
If you want to use the rule in the middle of the URL for an API like in my case, then use the first answer from #anubhava. That worked for me.
Also, I'm using IIS so I had to install the Rewrite Module and then Import the rule from the first answer. Second and Third answers will work only for the last part of the URL.
I need to
Remove the file extension from all of the urls on my site (mywebsite.com/about.html >> mywebsite.com/about/)
Always add a trailing / to the end of the url (mywebsite.com/about >> mywebsite.com/about/)
Allow for an exception that one of the nav items links to a pdf, not an html document (mywebsite.com/calendar.pdf >> mywebsite.com/calendar/
Allow subdomains to go to their folder instead of being rewritten (My folder tree is public_html>my main site files and a dev folder>(inside dev folder) index.html) I need the subdomain to link to that dev folder, currently the url rewriting changes dev.mywebsite.com to public_html/dev.html
This is my current, hacked together htaccess file:
RewriteEngine On
RewriteRule ^calendar/?$ files/2013-2014_calendar.pdf
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Sorry, this might be a duplicate or semi-duplicate. I looked around as much as I could but couldn't find anything that applied to my situation exactly or that I understood. Thanks guys!
Try this code:
RewriteEngine On
RewriteBase /
# skip dev. subdomain
RewriteCond %{HTTP_HOST} ^dev\. [NC]
RewriteRule ^ - [L]
# calendar rule
RewriteRule ^calendar/?$ files/2013-2014_calendar.pdf [L]
# hide .html rules
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1/ [R=301,L,NE]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
I know this is a pretty common task and has been discussed here a lot, but I still can't get it to work. After doing some research I added this code into my .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
But it still doesn't work and displays the .html on my pages.
What could be wrong?
Thanks
You need an additional rule to externally redirect foo.html to foo. Have your .htaccess like this:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
I've used htaccess to remove extensions or redirect, such as;
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^(.*)/$ /$1.php [L]
I was wondering if anyone has used to it in this way:
If file extension can't be found, check for html or php file under the same name re-direct to file exith extension.
I can only find examples for redirection, file-extension removal and adding trailing slashes to the file, not to do something like my example, any ideas?
Try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# see if .php file can be found with same name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
# now see if .html file can be found with same name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
I am trying to create a nice looking url for my calendar site.
But I can't get it to work properly.
This is my URL:
http://mydomain.com/admin/calendar?year=2013&month=november
...and I want it to look like this:
http://mydomain.com/admin/calendar/2013/november
The "calender"-folder is not a real folder and the .htaccess file is located in the admin-folder and not in the root of my website.
I am currently using this code:
RewriteEngine On
# Make sure not to rewrite real files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# Removes PHP extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# Make new URL
RewriteBase /admin/
RewriteRule ^([^/]*)/([^/]*)$ /admin/calendar/calendar?year=$1&month=$2 [L]
Any idea why this doesn't work, and how to get it to work? :D
Thanks :)
TheYaXxE
Try this code in your admin/.htaccess file:
RewriteEngine On
RewriteBase /admin/
# Make sure not to rewrite real files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Make new URL
RewriteRule ^calendar/([^/]+)/([^/]*)/?$ calendar.php?year=$1&month=$2 [NC,L,QSA]
# Removes PHP extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+?)/?$ $1.php [L]
Try this:
RewriteRule ^([^/]+)/([^/]+)/$ /admin/calendar/calendar?year=$1&month=$2