My new rewrite breaks my forum login?

I would post this in the original thread http://www.sitepoint.com/forums/showthread.php?1013274-Need-help-tweaking-this-htacces-code-please - which would help future users much more - but someone closed it. Here is the code Jeff gave me. Works perfect except it prevents me from loging into my forum backend for some reason. Took me 15 hours to figure that out. OMG. I tried to give a exclusion to the forum folder but I must not of been doing it right because I couldnt get it to work.

So two questions? Why would this break my forum login? And two, can I make it exclude the forum located at www.site.com/forum/? Doing so I would assume all would function correctly again. FYI using my old and much larger rewrite the login works. Thanks.

# Rewrite index.html/php to folder
RewriteRule ^(.*/)?index\\.(html?|php)$ /$1 [R=301,L]

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

Without being able to see or reproduce the issue, my best guess is that somewhere the forum POSTs to an index.php page, and when it’s redirected to strip index.php out of the URL, then the POST data is lost… But that’s only a guess.

If you want to exclude your forum section from these rewrite rules, then this condition should suffice.

RewriteCond $1 !^forum(?:$|/)

This would go in front of each of the two rewrite rules. Keep in mind that this might not be the best solution, but without being able to troubleshoot, I can’t even be certain of the problem.

Thanks Jeff. Ya I’m pretty sure thats exactly what it’s doing - posting to the index. Is that above rule essentially saying exclude /forum? So directly off the root. Not that it is, but if it was …/…/forum then it wouldn’t work correct? Sorry to bug I just like to somewhat understand things.

And oh ya here was my other question. If these to rules (old and new) where doing the same thing just written differently then why does the old on work and this new set of rewrites not when it comes to this forum? Is it because the original rewrite only rewrote the root and not all the other folders?

# 301 permanent redirect index.php to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\\ /([^/]+/)*index\\.php\\ HTTP/
RewriteRule ^(([^/]+/)*)index\\.php$ http://www.website.com/$1 [R=301,L]

# 301 permanent redirect non-www (non-canonical) to www
RewriteCond %{HTTP_HOST} !^(www\\.website\\.com)?$
RewriteRule (.*) http://www.website.com/$1 [R=301,L] 

If it was indeed writing to the index then this is the “only” solution correct?

Yes.

If the web files for forum lived in some other location, then no, none of the rules in this htaccess would be applied. htaccess files are scoped to a filesystem directory. You’d either have to duplicate the htaccess for forum, or you’d have to move your rewrite rules into the server’s main configuration file. In the main configuration file, the rewrite rules could be applied in a per-server context rather than per-directory.

I’m afraid, again, that without being able to see or troubleshoot the issue, I can’t know why this is. But if the older rewrites work with your forum while the newer ones don’t, then that makes me less confident that my initial guess was correct.

Certainly not the only solution. Another possibility would be to exclude POST requests… which may actually be the better option.

Quicky! I believe my forum folder would be ok. Its only the adm/ folder that I was having the issues. adm is directly off the forum… site-root/forum/adm/. Can you please show me how to write that exclusion rule for one more deeper folder? Thanks! I would just guess but I have no idea what all the mumbo jumbo stuff is right after forum.

original…
RewriteCond $1 !^forum(?:$|/)

my guess 1…
RewriteCond $1 !^forum/adm(?:$|/)

my guess 2…
RewriteCond $1 !^adm(?:$|/forum)

Correct!

$|/ means end of string ($) or (|) a slash (/). And FONT=Courier New[/FONT] are grouping parentheses. They make sure that the or (|) applies to only what is inside the parentheses. So it will match “forum” followed by either a slash or the end of the string.

Awesome - thanks Jeff! :slight_smile: