| .htaccess and WordPress
|
leemon

msg:4169025 | 6:59 am on Jul 13, 2010 (gmt 0) | Hi! My mod_rewrite skills are almost non-existant and I need some help to setup a simple redirection on my WordPress .htaccess file:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{QUERY_STRING} !cat=24 RewriteRule . /index.php [L] </IfModule> # END WordPress
Redirect 301 /index.php?cat=1 http://domain.com/category/some_category/ Redirect 301 /index.php?cat=1&pic=1 http://domain.com/category/some_category/
The WordPress permalink rules conflict with the aforementioned rules and when I type http://domain.com/index.php?cat=1 or http://domain.com/index.php?cat=1&pic=1 on the URL bar, Apache redirects me to http://domain.com/?cat=1 and http://domain.com/?cat=1&pic=1 respectively instead of http://domain.com/category/some_category/ Can anyone help me, please? Thanks
|
lammert

msg:4169032 | 7:09 am on Jul 13, 2010 (gmt 0) | Apache rules are parsed and executed from top to bottom. If you want the two 301 redirects to take precedence over the default WordPress rules, you should move them above the #BEGIN WordPress line.
|
g1smd

msg:4169042 | 7:26 am on Jul 13, 2010 (gmt 0) | That's true, but because the .htaccess file is parsed by each Apache module in turn, all of the Redirects are executed and then all of the RewriteRules; however it might be that all of the RewriteRules are executed, and then all the Redirects. The latter situation will expose internal filepaths onto the web as URLs. You have no control over this. So, not only do you need to list all of the redirects first and the rewrites last, you should change the syntax of your redirects so that all of them use RewriteRule code with the [R=301,L] flags.
|
leemon

msg:4169055 | 7:46 am on Jul 13, 2010 (gmt 0) | Ok, I did what you both told me and now it works!
RewriteCond %{QUERY_STRING} ^cat=1((&[^&]+)*)&?$ RewriteRule ^index\.php$ http://domain.com/category/some_category/? [L,R=301]
Thanks!
|
jdMorgan

msg:4169259 | 2:38 pm on Jul 13, 2010 (gmt 0) | You can speed this up a bit:
RewriteEngine On RewriteBase / # RewriteCond %{QUERY_STRING} ^cat=1(&.+)?$ RewriteRule ^index\.php$ http://domain.com/category/some_category/? [L,R=301] # # BEGIN tweaked WordPress # RewriteCond $1 !(^index\.php|\.(gif|jpe?g|png|ico|css|js))$ RewriteCond %{QUERY_STRING} !^([^&]*&)*cat=24(&.+)?$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ /index.php [L] # # END WordPress
I'm not sure what that "!cat=24" exclusion is for. If it applies only to index.php requests, then that RewriteCond should not be needed, since the index.php URL-path is already excluded by the first RewriteCond. If it is needed, then the more-specific pattern shown prevents matches on "scat=24" and/or "cat=241". Excluding requests for the most-frequently-requested objects NOT generated by WP prevents the unnecessary and wasteful RewriteCond calls to the OS to check for "file or directory exists" on these requests. You may actually see a noticeable speed-up of your site by avoiding these checks when not needed. The bottom line on -d, -f, and the other "exists-checks" is that they should always be done last whenever possible and should only be done when absolutely necessary. The same is true for reverse-DNS lookups using "RewriteCond ${REMOTE_HOST}" which is also very-slow and resource-intensive function. The <IfModule> container is only needed if you want this code to fail silently on servers where mod_rewrite is not available. Otherwise, it's a waste of time and CPU... Jim
|
|
|