Forum Moderators: phranque

Message Too Old, No Replies

htaccess / rewriterule problems on my site - any ideas?

         

scottcabal

1:33 pm on Jun 20, 2012 (gmt 0)

10+ Year Member



I've spent the last 24 hours looking for solutions to my problem both here and by searching google but I've been unable to find anything. Sorry if this is duplicated anywhere however.

For the last version of my site, written in php, I just had one index page which I used requests like this to grab the correct page: I'm now writing the new version of the site, also in php, and I'm trying to make it have search engine friendly urls / slugs for example

/page/view/thispage
/account/login


I've managed to get it so that virtual subdomains work for each client, using the top portion of the code below, and I've managed to get the slug pages to work using the bottom portion.

The problem I'm having is with forms, such as the form to sign up for the site, edit a user profile etc. I've read on this site that with apache rewrites $_POST variables should be passed to the script, unlike with redirects, but I'm using rewrite and when any form data is passed to a slug (for example /profile/edit) it doesn't work, although if it's passed to virtualsubdomain.mydomain.com/test.php for example it does.

Can anybody help me out and find a way to fix the htaccess file below and allow $_POST vars to be passed to my slug script (index.php in the code below)? There's probably just a small thing I'm missing.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mydomainhere.org
RewriteCond %{HTTP_HOST} ^(.+).mydomainhere.org/^(.+)
RewriteRule ^([^/]*)$ [mydomainhere.org...] [P,L]

ErrorDocument 404 /index.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php
</IfModule>


I can't work out why post vars can be read by php files that exist in the subdomains that are only virtual, but they can't be read when post them to a slug (such as /profile/editprofile). Global variables are off on my server but The following just brings up the default blanked variable so they obviously havent been passed.

$test = ''; //define the variable to please global vars off
$test = $_POST["namehere"];
echo $test;


I'm fairly new to using htaccess files for anything other than just including a custom error file as you can probably tell.

g1smd

6:37 pm on Jun 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Start off by tidying the code.

The
ErrorDocument
directive should be first or last in the file, not in the middle of the mod_rewrite code.

Delete the
IfModule
tags.

RewriteEngine On
should appear only once.

In the
HTTP_HOST
conditions, escape all the literal periods.

Use example.com in this forum to suppress URL auto-linking.

Why are you using the
[P]
flag?

RewriteBase /
is the default and can be deleted.

In the
RewriteRule
use a more exact pattern than
(.*)
then you can dump the
-f
and
-d
tests. Those are very inefficient.

Every rule needs the
[L]
flag.

scottcabal

6:44 pm on Jun 20, 2012 (gmt 0)

10+ Year Member



I don't know what any of the flags do or indeed what most of the rule conditions mean... I spent ages trying to find code that worked for the first situation and eventualy found that online so just copied it straight from wherever I found it.

Same with the last one I just copied it straight from somewhere and changed my variables thats why it's untidy but I'll tidy it up and change what you suggested and see if that makes any difference.

I don't really understand what half the rules mean thats why (.*) is still there... but it does just need to use index.php for anything that's not found - that way index.php just takes the path and determines what the user was trying to access, and includes it from outside the public html directory.

g1smd

7:10 pm on Jun 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're now dealing with server configuration code, so a few days or weeks spent learning what this stuff does is time well spent. mod_rewrite offers concise and extremely powerful rules to do some really complex stuff in a couple of lines of code.

The mod_rewrite part of the Apache manual and a good RegEx tutorial are required reading. :)

lucy24

8:45 pm on Jun 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't know what any of the flags do or indeed what most of the rule conditions mean... I spent ages trying to find code that worked for the first situation and eventualy found that online so just copied it straight from wherever I found it.

If you want to bring your site crashing to the ground, that is a good way to do it.

Bookmark this page [httpd.apache.org]. That's the 2.2 version. If you are on 2.4, you can simply replace "2.2" with "2.4" in the URL. (I tried it.) If you are stuck on 1.3 the docs must exist somewhere, but they are hiding.

Regular Expressions come in different "flavors" (fancy word for dialects). A couple of small details are specific to certain Apache modules. But the most important thing is to find a tutorial and reference that you are personally comfortable reading, because you're going to spend a lot of time with it. DO NOT try to learn everything at once.

Sidenote: I've been looking at that mod_rewrite URL several times a day for over a year and have only just realized that it contains a period (full stop) smack dab in the middle, miles removed from any extension. If your name is Apache, you can get away with this. Normal humans should avoid it.