0

SITUATION: I have a file-directory-based website with old-fashioned URLs, that I'm trying to convert to a system that doesn't require so many files and directories.

So, a URL like this: www.mysite.org/members/support/newmember/

currently returns the following file: www.mysite.org/members/support/newmember/index.php

This works but creates a need for far too many directories and index files.

While developing another site for a buddy who had too much work, I got from him a script that uses mod_rewrite to parse an old-fashioned URL into $_GET[] variables. With this .htaccess script:

Options +FollowSymLinks RewriteEngine on RewriteRule ^([A-Z­a-z­0-9­-]+)/?$ index.php?folder=$1 [L] RewriteRule ^([A-Z­a-z­0-9­-]+)/([A-Z­a-z­0-9­-]+)/?$ index.php?folder=$1&page=$2 [L] RewriteRule ^([A-Z­a-z­0-9­-]+)/([A-Z­a-z­0-9­-]+)/([A-Z­a-z­0-9­-]+)?$ index.php?folder=$1&page=$2&subPage=$3 [L] 

and an old-fashioned URL like: www.mysite.org/members/support/newmember

my index file in my root directory has the following vars available to it: $_GET['folder'] $_GET['page'] $_GET['subPage']

and the script in the Index file simply assembles the correct includes according to the $_GET variables, thus eliminating the need for such an extensive file structure.

PROBLEM: Query strings are no longer available to the PHP script. Mod_rewrite has gobbled them up and spit out only the GET vars that are defined in the rules in .htaccess. This is okay for all the internal site links because I can abandon query strings and use my URL vars or SESSION vars to make values available to my pages. But the sticky issue remaining is that my site sends people to PayPal, and my scripts for that depend on the ? query string that PayPal sends back to my site. PayPal sends the user back with something like:

www.mysite.org/support/members/newmemberconfirmation?id=1234567

and the ?id value, even though it appears in the URL pane of the browser, isn't available to the script.

DESIRED SOLUTION: I don't know how to write scripts for mod-rewrite; I can't even make a regular expression very confidently. What I need is a script that assign the values between the slashes in the URL to GET vars, but will also make the original GET vars available to my PHP script. Anybody have a snippet for me that will do that?

3
  • stackoverflow.com/questions/10555589/… Commented Dec 10, 2016 at 16:58
  • 1
    You should start reading documentations and some basic "getting started" tutorials. All of that is explained all over the place. The documentation actually also offers those "snippets" you ask for... Commented Dec 10, 2016 at 17:10
  • There are dozens, if not hundreds of questions dealing with this already. You're doing it the hard way anr that's what caused your problem in the first place. I'm reluctant to mark a question as a duplicate when the other's solution is average. Send everything to index.php in one rule and you'll see that $_SERVER['REQUEST_URI'] contains what you need. Commented Dec 10, 2016 at 20:55

1 Answer 1

0

Change 3 flags [L] and use [L,QSA]:

Options +FollowSymLinks RewriteEngine on RewriteRule ^([A-Z­a-z­0-9­-]+)/?$ index.php?folder=$1 [L,QSA] RewriteRule ^([A-Z­a-z­0-9­-]+)/([A-Z­a-z­0-9­-]+)/?$ index.php?folder=$1&page=$2 [L,QSA] RewriteRule ^([A-Z­a-z­0-9­-]+)/([A-Z­a-z­0-9­-]+)/([A-Z­a-z­0-9­-]+)?$ index.php?folder=$1&page=$2&subPage=$3 [L,QSA] 

QSA|qsappend
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

http://httpd.apache.org/docs/current/en/rewrite/flags.html#flag_qsa

Sign up to request clarification or add additional context in comments.

Comments