neiko
neiko

Reputation: 35

Rewriting url with one parameter out of multiple parameters in .htaccess

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]

Upvotes: 2

Views: 1357

Answers (2)

anubhava
anubhava

Reputation: 786091

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]

Upvotes: 1

Phil Perry
Phil Perry

Reputation: 2130

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.

Upvotes: 0

Related Questions