0

I have the following .htaccess file

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]

RewriteRule ^constant?([^/]+)=([^/]+) /index.php?url=constant&type=$1&task=$2 [L]

The last line isn't quite working or is being affected by the previous.

If $_GET['url'] doens't exist in DB it fires 404.

contan?x=y //gives 404
constant?x=y //works

That is how it shoud work.

However, when I use var_dump($_GET); on line 1 of index at example.com/constant?x=y

I only get array(1) { ["url"]=> string(8) "constant" }

Is the last line being affected by the previous, could someone point me in the right direction?

2
  • TBH I'm lost as what are you trying to achieve. This rule RewriteRule ^constant?([^/]+)=([^/]+) does not make much sense for me -- it's wrong. Could you please tell how would you like to rewrite that URL (from URL => to URL) and I will do the rule for you. Also -- you are saying: { ["url"]=> string(8) "constant" } -- that is correct. If not -- what do you expect there? Commented Sep 12, 2011 at 21:19
  • I was expecting : { ["url"]=> string(8) "constant", ["type"]=>..., ["task"]=>..., }. You're right about the RewriteRule I was trying to make a very simple url so : example.com/index.php?a=x&b=y&c=z to example.com/x?y=z. Which is misusing ? and =. The server see it as $_GET['y'] == z. Is something like this achievable? Or as simple? Thanks. Commented Sep 12, 2011 at 23:35

3 Answers 3

3

You need to use the query string append flag, QSA, to ensure you pass the existing GET vars. Otherwise, they are overwritten by the RewriteRule

RewriteRule ^constant?([^/]+)=([^/]+) /index.php?url=constant&type=$1&task=$2 [L,QSA]
Sign up to request clarification or add additional context in comments.

Comments

1

Use these rules:

Options +FollowSymlinks
RewriteEngine On

# specific rule
RewriteCond %{QUERY_STRING} ^([^=]+)=([^&]+)
RewriteRule ^(constant)$ /index.php?url=$1&type=%1&task=%2 [L]

# general catch-all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]

You cannot match query string in rewrite rule: rewrite rule only matches path part of the URL while query string has to be matched via rewrite condition.

Obviously, this specific rule will not be triggered if you do not provide enough parameters, e.g. /constant or /constant?say=.


If you add QSA flag to that rule, then you will be able to pass other parameters as well (but it will pass whole query string over): e.g. /constant?say=meow&loud=yes will become /index.php?url=constant&type=say&task=meow&say=meow&loud=yes -- it can be useful depending on your URLs and tasks performed:

RewriteRule ^(constant)$ /index.php?url=$1&type=%1&task=%2 [QSA,L]

Comments

0

Since you are using the L flag in the first rewrite rule, and since that rule always matches, you will never trigger the last rule.

Try to put the last rule before the first rule (and add the QSA as Jason McCreary said).

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^constant?([^/]+)=([^/]+) index.php?url=constant&type=$1&task=$2 [L,QSA]
RewriteRule ^(.*)$ index.php?url=$1 [L]

3 Comments

This would give me array(1) { ["url"] string(9) "index.php"; } (404) on everypage. Thanks though!
I think the slash before the index.php shouldn't be there. I removed it from my answer, please try it again.
Still has same effect as before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.