.Htaccess template questions

Hello all,

I have made a .htaccess file that servers as a template for future websites.
Still I’m having problems with some points.

  1. I would like to have trailing slashes in my urls so I need to link “domain/link” to “domain/link/” and this also applies to the “domain/link/link/” form
  2. I to disable index.php direct access. So “domain/index.php” or “domain/index.php?request=about” shouldn’t be allowed but “domain/about/” should.

Here’s my .htaccess file.

Options -Indexes
ServerSignature Off

<files .htaccess>
deny from all
</files>

<ifModule mod_php5.c>
php_value zlib.output_compression 16386
</ifModule>

<IfModule mod_deflate.c>
<FilesMatch "\\.(css|html|js|php|txt|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]

# With the www. prefix
RewriteCond %{HTTP_HOST} ^example\\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# Without the www. prefix
#RewriteCond %{HTTP_HOST} ^www\\.(.+)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]

Also, if you have any suggestions for the existing code, please respond! :smiley:

Thank you in advance,
xtaste

Perhaps not a big deal, but just something to consider… even through rewrite rules get the job done, the Apache docs recommend that redirections be handled with the [URL=“http://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect”]Redirect directive.

xt,

If you are a host, you would know that:

  1. Your attempt to force trailing slashes on non-directories is an extremely bad idea.
  2. The assumption that omitting a (DirectoryIndex) filename and using a “directory” and expecting it to be assigned as the value of ANY key and redirected to the presumed DirectoryIndex file is (IMHO) nonsensical.
  3. Using .htaccess to constantly repeat <IfModule> tests multiple times for every request is abusive of a server.
  4. Writing loopy code (note that the www’s are contradictory) is … well, loopy.

At this point, please allow me to recommend that you reconsider your motive for your ill-conceived questions rather than pursue this exercise.

Regards,

DK

The problem is that google recognizes the url with the trailing slash and the url without the trailing slash as two separate urls. From what I’ve read, it seems to be a good idea to force urls one way or the other, so long as the url isn’t a file name with an extension, or doesn’t have a trailing url query.

Writing loopy code (note that the www’s are contradictory) is … well, loopy.
It’s commented out.

xtaste, are you using a CMS of any sort? (wordpress, joomla, drupal, etc)

Hello all,

Sorry for my late reply, my pc wouldn’t boot anymore, it’s temporarily fixed.

@ Jeff Mott: Thanks for your input! That’s something new to me, thanks! But, how to use it in my case? Can I use it to redirect http:// to http://www. (and the other way around)? Or can I use it to redirect everything to index.php?request=example?

@ dklynn: I have read many posts from google and did my research so…

  1. It’s not a bad idea, on the contrary, Google prefers it that way unless you’re talking about files (so, with file extension e.g. .jpg, .php, .html, etc)
  2. Sorry, I don’t really understand what you mean.
  3. That’s true, but I can’t see any other way to test if these modules exist. Do you have an alternative? If this htaccess file would be placed on a server that doesn’t meet the requirements for the modules and the if-statements wouldn’t be there, it would be far more server-heavy, because it would cause errors… or am I wrong?
  4. One of the redirects is commented out. It’s this way because it’s a template, it must contain all necessary code that can do both; with www and without. I comment out or remove the lines that aren’t needed for a website when the website is finished.

@ Force Flow: Thanks for your reply! I am using my own framework, it takes the request parameter as its input to the bootstrap file.

Sorry again for my late response,

Kind regards,
xtaste

Hey all,

I’ve been working on the .htaccess file today. This is what I have now, please let me know what you think.


Options +FollowSymLinks
Options -Indexes
ServerSignature Off

<files .htaccess>
deny from all
</files>
<files config.php>
deny from all
</files>

RewriteEngine On

# BASE PATH
RewriteBase /

# i've added on more possible harmful bot
RewriteCond %{HTTP_USER_AGENT} ^libwwwperl [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^libwww-perl [NC]
RewriteRule .* - [F,L]

# IP FIX
#RewriteCond %{HTTP_HOST} ^([.0-9]+)$ [NC]
#RewriteRule .* http://www.example.com/ [L,R=301]

# WWW. ON
#RewriteCond %{HTTP_HOST} !^www\\. [NC]
#RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# WWW. OFF
#RewriteCond %{HTTP_HOST} ^www\\.(.+)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

# this is for the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !.*/$ [NC]
RewriteRule ^(.*)$ $1/ [L,R=301]

# the redirect to the index.php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]

I’ve tried to make the www. version as short as the non-www. version, but that doesn’t work.


# WWW. ON
#RewriteCond %{HTTP_HOST} !^www\\.(.+)$ [NC]
#RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]

This is probably because of the NOT (!) factor.

The index.php?request=example problem has been fixed by just adding a canonical meta tag.

Any suggestions or corrections?

Thanks in advance,
xtaste

Correct. %1 would be the value matched in the capturing parentheses… except that the condition passes only if the pattern doesn’t match. Replacing %1 with %{HTTP_HOST} should probably do the trick. Also, consider copying some examples from the Apache docs.

Hey Jeff,

Thanks for your reply. I’ll try the %{HTTP_HOST} method out tomorrow! That would give me some more freedom in making the htaccess.
Thanks for the link, I’ll give it a try, I’ll report tomorrow how it all worked out.

Kind regards,
xtaste

Hey Jeff,

Thanks for all the help and the Apache docs helped me better understand some parts and made it even easier.
I still stick with the mod rewrite as I want to be as dynamic as possible, so no www.example.com replacements anymore :slight_smile:

I use this code for the www. prefix.


RewriteCond %{HTTP_HOST} !^www\\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

It works great, but when I read the Apache docs I noticed they also gave this option but with the following line added.


RewriteCond %{HTTP_HOST} !^$

This means the string should not be empty, but if it was they wouldn’t be able to visit my website, right?
I don’t really understand what’s going on here.

I also have questions about the NE and R ‘tags’. R should contain a code like 301 or 302, right? And doesn’t the NE ‘tag’ complicate things when you have special characters in your urls? (not that I’m fond of using these in an url).

Thanks again,

Kind regards,
xtaste

Very nice. :slight_smile:

I can’t think of any scenario either where the host could be blank. My best guess is that during internal testing, they were able to simulate a request with a blank host and needed this line to pass all their test cases.

The R flag can contain a code like 301 or 302. If it’s left off, then the default will be 302, a temporary redirect.

My best guess for NE is that they thought it might be a common scenario where the new, redirected URL would contain a hash mark, like in their NE flag example. But you seem to be right also, that if, for example, the URL contained an escaped hash mark, then the NE flag would complicate things.

Hey Jeff,

Thanks for all the replies, no more questions here! :smiley:

Kind regards,
xtaste