Forum Moderators: phranque
My htaccess file appears to be doing everything I want except I can not get
[domain.com...]
to do a 301 redirect to
[domain.com...]
Can someone please tell me what I am doing wrong?
NOTE REGARDING MY DUE DILLIGENCE
I have spent the better part of today and yesterday finally trying to understand what htaccess files are. I have been studying WebmasterWorld, and the apache site and have learned that;
- htaccess is a second choice when you can not get access to the main server configuration file.
- you can put an htaccess into any directory you want
- putting a # in front of a line comments it
- you can use it for error messages
- you can use it to redirect
I still can not find anywhere that teaches the syntax in a way that I understand. The apache site shows the syntax at
[httpd.apache.org...]
I have also read the charter for the forum and am trying not to do a "Code Dump". I do not know exactly which part of my code would be the problem. I am guessing that there is something wrong with the
RewriteRule . /index.php [L]
line, but I am not sure.
So here is the code, and I would like to tweak it so that it also redirects http://www.example.com/index.php to http://www.example.com/
I would love to hear the correct correction to my code, and I would also like to be pointed to the source of data as to why if possible.
Thank You! dk
==========================================================
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# My Attempt :)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^/index\.php$ http://www.example.com/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Your "attempt" is fine, with one exception:
You should reverse the order of the two major code blocks, so that all of the redirects are done before your rewrites to wordpress. Otherwise, wordpress URLs will not benefit from the "fix-ups" you are doing with your redirects. This is likely the reason that your code isn't working.
You did not mention your second new rule, which redirects "index.html" to "/", or the third, which does domain canonicalization, so I assume that you have no questions about those.
I believe that if you move your new rules above the wordpress rules, upload the modified code, flush your browser cache, and test, that you will be pleased.
To answer part of a question in another recent post: Print out this code, and then read each word or regular-expressions token. Mark each word or character that you understand with a yellow magic marker. Do not allow yourself to proceed past an unmarked word or character until you have looked it up in the Apache mod_rewrite documentation or in a regular-expressions tutorial, as applicable, and can honestly tell yourself that you understand it. It's hell for the first 15 minutes, and then gets a lot easier. Repeat as required.
Don't start with the code in the thread you cited at the top -- That code is designed to address a very specific application and is very complex because of that application and a bug in Apache mod_rewrite. It is four times more complex that normal code because of the very-demanding design requirements, and is actually a very poor example of typical mod_rewrite code (I can say that).
Two tips about mod_rewrite code: First, there are two main parts; the mod_rewrite directives, and the regular expressions that they use. Each is a small, powerful thing, and the two combined are very powerful. The second tip is that it is MUCH harder to read regular expressions than to write them, unless they are documented in plain language. So the effect is usually that understanding comes slowly at first, and then in a flash, and soon you're coding your own solutions without much effort. Frankly, it's the typos that always give me the most trouble... :)
Links to mod_rewrite and regular-expressions tutorials are in our forum charter and forum library, and there are many others on the Web.
<IfModule mod_rewrite.c>
#
RewriteEngine on
RewriteBase /
#
# My Attempt
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^/index\.php$ http://www.example.com/ [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#
</IfModule>
# END WordPress
Jim
It still does not work for the http://www.example.com/index.php
Any suggestions?
Also, where on the Apache site can I find the definition of the words and terms? I may be blind but I can not figure out where to begin on the site for a new learner.
Thanks as always!
dk
===================================================================
# My Attempt :)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^/index\.php$ http://www.example.com/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# End of my attempt
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
<IfModule mod_rewrite.c>
#
RewriteEngine on
RewriteBase /
#
# My Attempt
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteR[b]ule ^ind[/b]ex\.php$ http://www.example.com/ [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /index.php [L]
#
</IfModule>
# END WordPress
If you want to redirect requests for all /index.php pages, as we're doing with the /index.html pages, then you can replace the first two rules with one:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html¦php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html¦php)$ http://www.example.com/$1 [R=301,L]
For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim
That should work for index.php or .html or .htm in any folder, or sub-folder, simply trimming the filename off the path, and ending with a trailing "/" on the URL, retaining the rest of the original folder path information.