1

I'm new with htaccess file, so I'm kindly asking you to help:

In my htaccess file I have next lines:

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^alpe-adria(.*)$ site/index.php$1 [L]
RewriteRule ^alpe-adria$ site/index.php?box=1&d=3 [L]

So, first I'm trying to set index file, that is in site folder, to show as string above (alpe-adria). Second RewriteRule is my landing page, other pages have same root as seen in the first RewriteRule. When I localy test this rules, my landing page shows as expected (typing localhost/alpe-adria in browser). But then if I want to go to other pages, which have for instance such basic URL:

http://localhost/site/index.php?d=1&box=1&grp=306&level=1

RewriteRule acts OK, rewriting this into

'http://localhost/alpe-adria?d=1&box=1&grp=306&level=1'

but site stil stays on my landing page - like it is written in second RewriteRule

I was also trying to set some more RewriteCond and adding another RewriteRule:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^alpe-adria?d=([^/]+)&box=([^/]+)&grp=([^/]+)&level=([^/]+)$ site/index.php?d=$1&box=$2&grp=$3&level=$4

and site stil doesn't change. What am I doing wrong? Thanks for any help :)

4
  • Try to explain it again what exactly are you looking for. Commented Nov 19, 2013 at 9:40
  • Are you sure it's a problem with the .htaccess not your code? What do you mean that the site doesn't change? Maybe it's something in index.php which wrongly recognizes the attributes. Commented Nov 19, 2013 at 9:41
  • After dumping $_GET in my index.php I get same parameters as in my landing page (box=1 and d=3) even if I change url to go to my subpages, which have more parameters. Case: URL = http://localhost/alpe-adria?d=1&box=1&grp=3&level=1 _GET dump -> array (size=2) 'box' => string '1' (length=1) 'd' => string '3' (length=1) Commented Nov 19, 2013 at 9:49
  • I suspect that second RewriteRule overrides other parameters and so it sticks to these parameters, no matter what I change in URL Commented Nov 19, 2013 at 10:00

1 Answer 1

0

Why not just use this rule (without any conditions)?

RewriteCond %{QUERY_STRING} box=
RewriteRule ^alpe-adria$ /site/index.php [L]

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^alpe-adria$ /site/index.php?box=1&d=3 [L]

The query string (everything after the ?) will automatically get appended to the end if you don't have a ? in your rule's target. The reason why your rules wasn't working is probably the conditions:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^alpe-adria(.*)$ site/index.php$1 [L]

I'm suspecting that you don't actually have a file called "/alpe-adria.php", thus the condition fails, and thus the rule is never applied (otherwise, that one might have worked).

Your second rule, which has no conditions, is simply:

RewriteRule ^alpe-adria$ site/index.php?box=1&d=3 [L]

And this one doesn't append the query string, since you are hardcoding it to "box=1" and "d=3".

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

5 Comments

After using your first rule - now parameters works only if they are written in url. What I realy want to do is to show my landing page calling only localhost/alpe-adria in url (that's why I hardcoded parameters in second rule) and also show other pages, which have parameters - like localhost/alpe-adria?d=1&box=1&grp=306&level=1. Problem is that my landing page also needs parameters (box=1&d=3) to show itself in browser. I still cannot figure it out how to combine both rewrite rules
@user3008034 Ah, I see. See the edit above. The problem is that you can't match against the query string in a rewrite rule, only against the %{QUERY_STRING} or %{THE_REQUEST} variables
One more question: how to single out one variable in %{QUERY_STRING}, so that I can use another RewriteRule for my subpage. I have dificulties to single out parameter grp, which is essential to show subapages to landing page. Correct me if I'm wrong, is it true, if I want to single out two parameters, I just need to add two RewriteCond %{QUERY_STRING} statements, each finding one parameter? Thanks in advance :)
@user3008034 RewriteCond %{QUERY_STRING} (^|&)box=([0-9]+)(&|$) then you can backreference it using %2 (the [0-9] part)
And if I want to get exactly one variable from parameter (apart from all other parameters), for example grp=something, then I just have to add this RewriteCond %{QUERY_STRING} (^|&)grp=something(&|$)?

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.