Jump to content

mod_rewrite and $_GET variable question


ifm1989

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/171598-mod_rewrite-and-_get-variable-question/
Share on other sites

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.

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>

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>

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.

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

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>

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

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>

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.