ifm1989 Posted August 24, 2009 Share Posted August 24, 2009 Currently, when something like this is loaded on my website: example.com/home It is loading something like this: example.com/load.php?open=home.php Then, a script on load.php will find the home.php file and require it (or generate a 404). Now, the inherent problem with this is that using additional URL variable won't work. In this example: example.com/home?foo=bar The URL variable foo=bar is being ignored, presumably because there have already been url variable declared. In reality, the url is lookign something like: example.com/load.php?open=home.php?foo=bar ...so it's no suprise this won't work. Are there any work arounds to this? I would like to avoid having example.com/load.php?open=home.php&foo=bar become the standard url look for my website. Any url rewrite advice would be golden. My primary objective would be to get example.com/file-id to mod_rewrite itself to example.com/load.php?open=file-id and still support additional url variables. Please let me know if this is possible, and if not, any suggestions you may have. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/ Share on other sites More sharing options...
Garethp Posted August 24, 2009 Share Posted August 24, 2009 Well why not give us the code that your using in your rewrite? Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904872 Share on other sites More sharing options...
Garethp Posted August 24, 2009 Share Posted August 24, 2009 Try ^([^/\.]+).php?([^/\.]+)$ index.php?open=$1&$2 Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904873 Share on other sites More sharing options...
ifm1989 Posted August 24, 2009 Author Share Posted August 24, 2009 Well why not give us the code that your using in your rewrite? Sure, but right now it's just test code to see if I can get this working. <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^home$ load.php?open=home.php [L] </IfModule> Again, example.com/home?foo=bar will not initialize the foo variable in the $_GET array. Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904874 Share on other sites More sharing options...
ifm1989 Posted August 24, 2009 Author Share Posted August 24, 2009 Try ^([^/\.]+).php?([^/\.]+)$ index.php?open=$1&$2 Hah! That crashed my WAMP server. More specifically, a slight modification did: <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^([^/\.]+)?([^/\.]+)$ load.php?open=$1&$2 </IfModule> Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904875 Share on other sites More sharing options...
Garethp Posted August 24, 2009 Share Posted August 24, 2009 Change RewriteRule ^home$ load.php?open=home.php [L] With RewriteRule ^home?([^/\.]+)$ load.php?open=home.php&$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904876 Share on other sites More sharing options...
ifm1989 Posted August 24, 2009 Author Share Posted August 24, 2009 Change RewriteRule ^home$ load.php?open=home.php [L] With RewriteRule ^home?([^/\.]+)$ load.php?open=home.php&$1 [L] The problem with this is I have to put a million rules into .htaccess. I'm going to build upon your code you provided above. What would be nice is to have something like this: example.com/value => exampe.com/load.php?open=value example.com/value/foo=bar&test=true => exampe.com/load.php?open=value&foo=bar&test=true This doesn't seem to work though (crashes my wamp server): <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^([^/\.]+)/([^/\.]+)$ load.php?open=$1&$2 </IfModule> Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904879 Share on other sites More sharing options...
Garethp Posted August 24, 2009 Share Posted August 24, 2009 That rule won't match the first example. It only matches example.com/value/Some other value Use <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^([^/\.]+)/([^/\.]+)$ load.php?open=$1&$2 RewriteRule ^([^/\.]+)$ load.php?open=$1 </IfModule> On second thought, this might work <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^([^/\.]+)/?([^/\.]+)$ load.php?open=$1&$2 </IfModule> Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904880 Share on other sites More sharing options...
ifm1989 Posted August 24, 2009 Author Share Posted August 24, 2009 Both suggestions crashed apache. I was using the following test url: http://localhost/home/test=test Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904882 Share on other sites More sharing options...
Garethp Posted August 24, 2009 Share Posted August 24, 2009 Test that on this <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^home\/([^/\.]+)$ load.php?open=home&$2 </IfModule> Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904883 Share on other sites More sharing options...
Garethp Posted August 24, 2009 Share Posted August 24, 2009 Sorry, it should be <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^home\/([^/\.]+)$ load.php?open=home&$1 </IfModule> I am waaay too tired. Bed time for me Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904884 Share on other sites More sharing options...
ifm1989 Posted August 24, 2009 Author Share Posted August 24, 2009 Ok, using this: <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^([^/\.]+)$ load.php?open=$1 RewriteRule ^([^/\.]+)/([^/\.]+)$ load.php?open=$1&$2 </IfModule> WORKS: http://localhost/home BROKEN: http://localhost/home/ WORKS ($_GET variables work too): http://localhost/home/test=test BROKEN: http://localhost/home/randomtext.html WORKS: http://localhost/randomtxt I'm happy with the last one. randomtxt is not a recognized file identifcation, so it gives the proper error. Just so you have an idea how this is working, load.php takes the $_GET['open'] variable and check it across a database to find a page url. For example, 'home' => 'index.php' EDIT: I was able to fix http://localhost/home/ by changing the rewrite code to <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^([^/\.]+)$ load.php?open=$1 RewriteRule ^([^/\.]+)/$ load.php?open=$1 RewriteRule ^([^/\.]+)/([^/\.]+)$ load.php?open=$1&$2 </IfModule> All I need is something to deal with the http://localhost/home/randomtext.html exception...and then I should be good. Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904886 Share on other sites More sharing options...
ifm1989 Posted August 24, 2009 Author Share Posted August 24, 2009 I also found this isn't working: http://localhost/?test=who Since nothing has been provided for "open", I'd like it to default to home. I tried to use this rewrite rule to get it to work...but no luck: RewriteRule ^/?([^/\.]+)$ load.php?open=home&$1 Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-904912 Share on other sites More sharing options...
ifm1989 Posted August 24, 2009 Author Share Posted August 24, 2009 Bump for justice Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-905251 Share on other sites More sharing options...
wildteen88 Posted August 24, 2009 Share Posted August 24, 2009 To pass on the url query string (?foo=bar etc) you should use the QSA flag RewriteRule ^([^/\.]+)$ load.php?open=$1 [QSA,L] Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-905330 Share on other sites More sharing options...
ifm1989 Posted August 24, 2009 Author Share Posted August 24, 2009 Thanks, it seems to be working beautifully. Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-905416 Share on other sites More sharing options...
ifm1989 Posted August 24, 2009 Author Share Posted August 24, 2009 Hmm, I do seem to have a problem. http://localhost/images/favicon.png Loading that link causes an error. Is there a way to have my rewrite rules apply only to the base directory...or if no / has been given? <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^([^/\.]+)$ load.php?page=$1 [QSA,L] </IfModule> Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-905428 Share on other sites More sharing options...
Garethp Posted August 25, 2009 Share Posted August 25, 2009 His suggestion would work, but use this if you come accross some errors in his code (I don't know much about QSA) <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^([^/\.]+)/?$ load.php?open=$1 RewriteRule ^([^/\.]+)/\??([^/\.]+)$ load.php?open=$1&$2 </IfModule> Anyway, mod Rewrite is basically just Regex, so you should look into this tutorial http://www.regular-expressions.info/tutorial.html Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-905500 Share on other sites More sharing options...
wildteen88 Posted August 25, 2009 Share Posted August 25, 2009 His suggestion would work, but use this if you come accross some errors in his code (I don't know much about QSA) QSA stands for Query String Append. I do not recommend your solution. Hmm, I do seem to have a problem. http://localhost/images/favicon.png Loading that link causes an error. Is there a way to have my rewrite rules apply only to the base directory...or if no / has been given? <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^([^/\.]+)$ load.php?page=$1 [QSA,L] </IfModule> This is because your rewrite rule is now sending all requests to load.php?page= You need to add a condition to only apply the rule if the request is not an existing folder or file. Like so <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/\.]+)$ load.php?page=$1 [QSA,L] </IfModule> Quote Link to comment https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/#findComment-905970 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.