I have made a .htaccess file that servers as a template for future websites.
Still I’m having problems with some points.
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
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!
Your attempt to force trailing slashes on non-directories is an extremely bad idea.
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.
Using .htaccess to constantly repeat <IfModule> tests multiple times for every request is abusive of a server.
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.
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)
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…
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)
Sorry, I don’t really understand what you mean.
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?
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.
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.
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.
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
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).
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 flagcan 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.