I tested the code below on several Dreamhost domains, and it works, except on newer domains added circa 2012
RewriteEngine on
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
example that works
ryanve.com/resources/lame/ and ryanve.com/resources/lame.php both redirect to ryanve.com/resources/lame and the actual file that is served is lame.php
broken example
airve.com/test/file properly serves file.php but the redirects airve.com/test/file/ and airve.com/test/file.php don't work. Try them and you'll see they seem to be spitting out the internal absolute path. The same thing occurred when I tried each of the redirects independently and when I tried the basic redirect below.
Redirect 301 /test/file/ /test/file
There is nothing else in .htaccess. Any idea on what the issue is?
You are redirecting to $1 in both cases, and you don't have a RewriteBase directive to tell Apache where to root such a relative path.
Try adding a root slash before the $1 to anchor it to the root of your webserver, for instance:
RewriteRule ^(.*)$ /$1.php [L]
Related
I am currently building a PHP-based portfolio site without any frameworks whatsoever. I have created an index.php file in the root and a lot of folders namely /about, /contact, /portfolio and the like. Within each of those, I have a separate index.php file
I created a .htaccess file and in it, I have this code...
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
For some reason when I visit my site at example.com/index.php or example.com/about/index.php the .htaccess file is not working and removing it.
Any ideas why?
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
If the intention with these directives is to remove index.php from the requested URL then you are using the wrong rules! This rule would route a request for /something to index.php/something (passing the requested URL-path as path-info to index.php) providing /something does not map to a file or directory.
You have presumably structured your URLs so they map to filesystem directories from which the DirectoryIndex is served, so the above directives would seem to be entirely redundant. (?)
To remove index.php from the end of any URL
To remove index.php (the DirectoryIndex) from any URL you would need to do something like the following instead:
RewriteRule ^(.+/)?index\.php$ /$1 [R=301,L]
Test first with a 302 (temporary) redirect to avoid potential caching issues.
To clarify...
you must not be linking internally to /index.php or /about/index.php. The above directive is only for when a user or external site erroneously requests the index.php file directly.
And include trailing slashes on your internal links. eg. you should be internally linking to /about/ and /contact/ etc. (not /about or /contact), otherwise mod_dir will implicitly issue a 301 redirect to append the trailing slash.
It took a while and thanks for the suggestions by Mr White this is my solution..
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Works perfectly now.
Thank you for everyone who took the time out to help me on this.
I've been working on my HTAccess for a couple days now, and I've hit a dead end.
I've rewritten and redirected the files to be extensionless, now I need to rewrite the url to be SEO Friendly.
Previously, the URL was: http://www.example.com/member.php?playername=encodedName
I removed the extension: http://www.example.com/member?playername=encodedName
I can't quite get it to: http://www.example.com/member/encodedName
Here's what I've gotten so far:
Code trying to redirect to SEO Friendly URL. Does NOT work.
RewriteRule ^member/([^/]*)$ /member.php?playername=$1 [L]
RewriteRule ^squad/([^/]*)$ /squad.php?squadname=$1 [L]
RewriteRule ^article/([^/]*)$ /article.php?articlename=$1 [L]
Unless directory, remove trailing slash. Works.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
Resolve .php file for extensionless php urls. Works.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Redirect external .php requests to extensionless url. Works.
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php $1 [R=301,L]
Does anyone have an idea as to what I am doing wrong? It's probably a very newbie problem that I just haven't encountered yet.
The "Code trying to redirect to SEO Friendly URL" part looks fine. And while you're redirecting requests made directly to php files to remove the extension, you're not doing anything specifically about the 3 SEO friendly URLs that you are rewriting back.
If when you go to http://www.example.com/member/encodedName, and you're not being served the content at http://www.example.com/member.php?playername=encodedName, then this looks like a Multiviews problem. You'll need to turn off Multiviews either in your server/vhost config or add this to the top of the htaccess file (or in the appropriate already existing Options statement):
Options -Multiviews
In order to do the redirecting to the SEO friendly URLs, add this before the "Redirect external .php requests to extensionless url." rules:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /member\.php\?playername=([^&\ ]+)([^\ ]*)
RewriteRule ^ /member/%1?%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /squad\.php\?squadname=([^&\ ]+)([^\ ]*)
RewriteRule ^ /squad/%1?%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /artcile\.php\?articlename=([^&\ ]+)([^\ ]*)
RewriteRule ^ /article/%1?%2 [L,R=301]
I have recently changed my website url using htaccess so that my urls will not show file extensions. Now my problem is as I have created a new xml sitemap so that my url will be extensionless!!! the Google webmaster tool is telling me about duplicate content issue!! ie. page and page.html have same title.... so my question is how do i redirect the urls with file extension html to urls with out extension!!!
this is an example of my website url with html extension
http://www.shenazhpeyk.co.uk/coding-machines.html
I want to redirect and change it to
http://www.shenazhpeyk.co.uk/coding-machines
so that will fix the issue with Google webmaster tools (Please provide me a code for use in htaccess file)
Many Thanks
Found this code as well. Not sure if it will accomplish the same thing. Seems to work for me as does the one above (for PHP).
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
I am wondering about the trailing slashes and whether those should be there or omitted?
Try adding the following to the .htaccess file in the root directory of your site redirect URLs with .html extension and remove it.
RewriteEngine on
RewriteBase /
#redirect to remove the .html extension
RewriteRule ^(.+)\.html$ $1 [L,NC,R=301]
Try this:
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
I want to make all my URLs uniformly clean. Which means all my URLs have no extensions, nor trailing slash, and if a person does enter in a .php or trailing slash, it would simply redirect the person to the clean URL.
Example:
example.com/blog/file.php
and
example.com/blog/file/
would both redirect to
example.com/blog/file
In .htaccess try this:
RewriteEngine on
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]
My web host recently (Sept 2011) updated their Apache servers to a newer version of Apache, and my old .htaccess code to display extensionless URLs no longer worked. After a considerable amount of copying code (that probably worked with older versions of the Apache OS) and unsuccessfully testing, I was able to determine that the following code seems to work on my server, and several others:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.com/$1 [R=301,L]
Give that a try - the -Multiviews seemed to be the key element to get it to work.
# Turn off the need of directory slash redirecting
DirectorySlash Off
# Turn on RewriteEngine
RewriteEngine On
# Internally redirect request to php file
RewriteCond "%{REQUEST_FILENAME}" !-f
RewriteCond "%{REQUEST_FILENAME}\.php" -f
RewriteRule "(.*)" "$1.php" [L]
# External redirects
# The above line is used to determine whether a internal redirect is done or not.
# If it is internally redirected, REDIRECT_STATUS will become 200.
# RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
# Redirect request with .php extension to extensionless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteRule "(.*)\.php$" "$1" [R=301]
# Redirect request with trailing slash to slashless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteCond "%{REQUSET_FILENAME} -d
RewriteCond "%{REQUSET_FILENAME} !-f
RewriteRule "(.*)\/" "$1" [R=301]
I've successfully modified my .htaccess file to remove trailing slashes on most pages but I'm wondering how to exempt my home page/directory? For example:
domain.com/test/ successfully redirects to domain.com/test
HOWEVER, when I hit my domain it will append the root document
domain.com/ redirects to domain.com/index.php
Is there a condition that I can add to ignore root url trailing slash so that it doesn't attempt to remove the trailing slash and add my default script? Here's what I have so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
OK. After a bunch of trial and error I answered my own question.
The third line denotes that there has to be something in the URI in order to perform the redirect thus not redirecting if the url just contains the initial slash.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://www.domain.com/$1 [R=301,L]
Redirects request for all URLs ending in a / except for the root:
RedirectMatch 301 ^(.+)/$ $1
How about
RewriteEngine On
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1