2

I have next lines in a htaccess file:

RewriteCond %{QUERY_STRING} (^|&)grp=([0-9]+)(&|$)
RewriteCond %{QUERY_STRING} (^|&)level=([0-9]+)(&|$) 
RewriteRule ^alpe-adria/([a-zA-Z0-9_-]+)$ /site/index.php?d=1&box=1&grp=%1&grpname=$1&level=%2 [L]

The original URL has this code:

/site/index.php?d=1&box=1&grp=13&grpname=art-culture&level=1

The parameters d and box are fixed, other parameters grp, grpname and level are varying from page to page. What I want to do is to rewrite this URL into such state:

/alpe-adria/art-culture

So the problem is, how to single out grpname parameter and also use all other parameters, from which two are not fixed. I tried two RewriteCond %{QUERY_STRING} rules with no success. Idea is to single out two parameters and then use them in original URL. In other hand one parameter is used in original URL as in substitute URL. Again, I'm stuck and I need your precious help.

Whole snippet:

<IfModule mod_rewrite.c>
RewriteEngine on

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

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

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

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

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

RewriteCond %{QUERY_STRING} (?:|.*&)grpname=([^&]*)
RewriteRule ^site/index\.php$ /alpe-adria/%1? [L,R]

2 Answers 2

1

Not very clear from your question but you can use a rule like this which makes your capture all these parameters grp, grpname and level from anywhere in query string:

RewriteEngine on

RewriteCond %{THE_REQUEST} \s/+site/index\.php\?d=1&box=1&grp=([^&]*)&grpname=([^&]*)&level=([^&]*) [NC]
RewriteRule ^ /alpe-adria/%2/%1/%3? [L,NE,R=302]

RewriteRule ^alpe-adria/([^/]+)/([^/]+)/([^/]+)/?$ /site/index.php?d=1&box=1&grp=$2&grpname=$1&level=$3 [L,QSA]
Sign up to request clarification or add additional context in comments.

20 Comments

Nice, but is it possible just to redirect into /alpe-adria/art-culture without other two parameters?
Now it shows me 404 error - site not found. Maybe because I have more code in htaccess before this snippet. Look at this link - stackoverflow.com/questions/20067729/… - the question I was asking before this problem occured. So, my whole snippet in htaccess file is:
Edited question above, since code doesn't look good in comments
Ok I think I got it now. How do you want /alpe-adria/art-culture to be handled internally? I believe it would be /site/index.php and some query string.
No you should not have that many rewrite rules. But to avoid that you cannot loose vital info while redirecting internal to pretty URI. At present it is: /alpe-adria/art-culture which has no info about level. Ideally your pretty URI scheme should be something like: /alpe-adria/<grp-name>/<grp-num>/<level> which will be good enough to be rewritten internally to correct URI in single rewrite rule.
|
0

Do you really have such a non-SEO non-SEF URI coming in from a browser? What is the point of converting it to an SEF form like '/alpe-adria/art-culture'? Unless your new directory structure is actually set up that way, and you're trying to match old non-SEO/SEF URIs to the new layout.

Assuming you actually have incoming '/alpe-adria/*' URIs, you're not rewriting the original URL into /alpe-adria/art-culture. You're using .htaccess to rewrite /alpe-adria/art-culture (coming in from a browser) into the original format that PHP and the server can digest. Please use the correct terminology. .htaccess does nothing to outbound URIs.

You're saying that /alpe-adria/ is fixed, and you have a 'search engine friendly' group name (e.g., 'art-culture'). You want to map 'art-culture' into a URI for index.php? I can't see where you are getting the grp and level entries from in the incoming URI. The best you're going to be able to do is to have a number of lookup entries: match 'art-culture', output the desired full URI. Match something else, output the different desired full URI.

Comments

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.