Dealing with multiple $_GET variables with mod_rewrite

Good afternoon all,

I am having a problem with passing 2 values through my URL and I think it’s something to do with my .htaccess file.

When any internal link on my site is clicked, I pass a $_GET variable and depending on that value, display the relative page.

I have an ‘Offers’ page containing 3 offers. There is a link for each offer and the idea is to populate my textarea on my contact page with the value of the offer, should they be linked to my contact page via the Offers page.

Trouble is, my .htaccess file I believe only deals with the instance of 1 $_GET value being passed, but anymore and it doesn’t work.

My is here:


RewriteEngine on
RewriteBase /
RewriteRule ^/?([a-zA-Z_]+)$ index.php?page=$1 [L]

How can I accommodate multiple values being passed?

The website link is here: http://www.tspv-websites.co.uk/Offer

Kind regards,

c0dingL!fe

You’ll need the query string append flag. “Appends any query string from the original request URL to any query string created in the rewrite target.”

RewriteRule ^/?([a-zA-Z_]+)$ index.php?page=$1 [L,QSA]

Good evening,

Thanks for the reply.

I’ve added your code and it doesn’t work for me.

How should I write the second value?

At the minute, this works for one variable:


<a href="./Contact">Contact</a>

And i’m trying to pass the second like:


<a href="./Contact/Deal1">Deal 1</a>

Kind regards,

c0dingL!fe

Good evening,

Update:

I’ve nearly got it working and the page loads, but for some reason the stylesheet is not being applied. Although when I look through developer tools, it is loaded in the <head>. No elements have any style whatsoever.

If you visit here: http://www.tspv-websites.co.uk/Offer and click on the Deal 1 button, you will see exactly what I mean.

Here is my code:


RewriteEngine on
RewriteBase /
RewriteRule ^/?([a-zA-Z_]+)$ index.php?page=$1 [L,QSA]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_0-9]+)$ index.php?page=$1&deal=$2 [L]

The URL looks good, the stylesheet is showing as loaded. This is very strange.

Anyone got any thoughts?

cL,

The problem you’re having is simply the change of directory level of the incoming request. When you redirect with multiple “entry” request directory levels, you either need the <base> tag or absolute URIs for your support pages. More in my signature’s tutorial.

As an aside, I’d recommend that you determine the Apache version you’re using to get rid of the optional /'s and your RewriteBase / is doing nothing (useful) do I’d think that should be deleted, too.

Regards,

DK

You’re rewriting most every URL to index.php, which means your CSS request is likely being rewritten as well. The simplest fix would be to exclude any real file from being rewritten.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_0-9]+)$ index.php?page=$1&deal=$2 [L]

Good morning both,

I used DK’s method by adding the <base> tag above all my external files in the <head> and it’s working perfectly now.

Thank you both very much.

Kind regards,

cL.