Forum Moderators: phranque

Message Too Old, No Replies

Simple Help with .htaccess please.

         

pwsstrat

4:23 am on Oct 16, 2010 (gmt 0)

10+ Year Member



Hello,

I've been reading on htaccess code and just can't seem to get the hang of it. What I want to do is simple; and if someone can give me an example for one URL I can apply it to the rest of my site.

Basically, I need a specific *.html to show when a specific url is visited.

Example below...

I want the contents of test-me.html to show when someone goes to /test-me/ on my domain name.

Better Example

[site.com...] will display the contents of test-me.html

I know we shouldn't use a trailing /, but with my current needs this is what I would like.

So, basically what code is required in the .htaccess file to see www.site.com/test-me/ display the contents of test-me.html

g1smd

6:59 am on Oct 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What have you tried so far?

You'll need a RewriteRule. The pattern will need to match exactly test-me/ and be start and end anchored. The internal target will be /test-me.html and it will need the [L] flag on the end.

pwsstrat

8:59 pm on Oct 16, 2010 (gmt 0)

10+ Year Member





RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]



However, it doesn't have the trailing /

g1smd

9:46 pm on Oct 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'll need a RewriteRule. The pattern will need to match exactly test-me/ and be start and end anchored. The internal target will be /test-me.html and it will need the [L] flag on the end.

So, put it all together and you get...

RewriteRule ^test-me/$ /test-me.html [L]


One line of code to rewrite the request to the internal file.

pwsstrat

5:07 am on Oct 23, 2010 (gmt 0)

10+ Year Member



Works perfect, however I can still load test-me.html; shouldn't it redirect test-me.html to test-me/ ? I would prefer it did this.

g1smd

6:23 am on Oct 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The code above is for the rewrite part of the solution - connecting the URL request with the file to deliver the content.

You need to add another two lines of code before that to also do the redirect. The first line of that code tests THE_REQUEST and if it matches GET /test-me.html HTTP/... the RewriteRule redirects from there to http://www.example.com/test-me. You'll need the [R=301,L] flags to set the 301 status, and the target is a URL with protocol and domain included, not just a path.

There are many thousands of examples of that code in this forum. It is the most asked question.

jdMorgan

4:49 pm on Oct 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code is obviously implementing a similar function, but not for .html files. The test-strings and substitutions in the internal-rewrite rule you posted are for .php files, not for .html...

These two rules were also incorrectly-ordered, which could have resulted in "exposing" your \.html filepaths to search engines and their subsequent appearance in search results.

I am discarding your second rule and replacing it with a new one, now shown as the first rule below. Your original second rule was intended to do something which you explicitly said that you do not want to do -- Although non-optimally coded, it was intended to remove trailing slashes from extensionless URLs.

There is also an exploitable security problem with your code which I will show corrected, but without further comment.

RewriteEngine on
RewriteBase /
#
# Externally redirect to remove ".html" and add a trailing
# slash to URLs directly-requested by HTTP clients
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/?#\ ]+/)*[^./?#\ ]+\.html([?#][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ http://www.example.com/$1/ [R=301,L]
#
# Internally rewrite requested URLs ending with a slash to the same-named .html
# files if those .html files exist and no directories of the same name exist.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(([^/]+/)*[^./]+)/$ /$1\.html [L]

Now be very careful... be very sure that all old links to 'real' .html pages on your site have been corrected to link to page-name-only-with-trailing-slash URLs before you deploy the first rule above.

In other words, all of the links on your site must specify the correct URLs that you wish to appear in search results. Leaving the links un-corrected and relying on the redirect invoked by the first rule would slow down your visitors' experience, make a mess of your log files and 'stats' reports, show as errors in Google Webmaster Tools, and result in your site being considered to be of "lower technical quality" by search engines.

If it isn't clear, using mod_rewrite involves far more that "just a little code to change my URLs." If you are not able to perfectly understand all the code and all of the search- and quality-related issues described above, I suggest that you do not use mod_rewrite until you can. One syntax error, incorrect or incomplete design requirement, or tiny typo in this code can either take down your server instantly, or worse, it can slowly and quietly ruin your search engine rankings over time and put you out of business...

The resources cited in our Apache Forum Charter may be helpful in this regard.

Jim