MOD_REWRITE for URL variables, deleting empty parameters and using more than 1 field for the first and second variables

I have a previous question that was answered very well, but a correction is required, and I could not answer, so I created a new question.

I need mod_rewrite, which did the following:

www.example.com?page=var1&cat=var2&subcat=var3&subsubcat=var4 

in it:

 www.example.com/var1/var2/var3/var4/ 

but also considered the optional use of var2, var3, and var4, so if these variables were empty, this would still work.

The answer to this question was very good, but a correction is required to change the parameters of the var2, var3 and var4 fields.

The code I have is:

 RewriteEngine on Options +FollowSymlinks -MultiViews RewriteCond %{QUERY_STRING} !^page= [NC] RewriteRule ^([^/]+)/?$ /?page=$1 [L] RewriteCond %{QUERY_STRING} !^page= [NC] RewriteRule ^([^/]+)/([^/]*)/?$ /?page=$1&cat=$2 [L] RewriteCond %{QUERY_STRING} !^page= [NC] RewriteRule ^([^/]+)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3 [L] RewriteCond %{QUERY_STRING} !^page= [NC] RewriteRule ^([^/]+)/([^/]*)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L] 

This works, but I need it to be able to do if-or-else to select which field is used for which variable and what makes it different.

For example, if we have the following two lines, it is easy to distinguish which field follows for which variable:

 www.example.com?page=var1&cat=var2&subcat=var3&subsubcat=var4 www.example.com?othervar1=var1&othervar2=var2&othervar3=var3&othervar4=var4 

but this line requires the ability to distinguish in which field the variable is located:

 www.example.com/var1/var2/var3/var4/ 

I need mod_rewrite to select which one is used.

Also, when I really go to the folder that exists, it works correctly. For example, if I go to a folder called "test", which is the actual folder:

 www.example.com/test/ 

he uses it as a variable for the page field. If I use the following without an end slash:

 www.example.com/test 

it outputs the following to the url:

 www.example.com/test/?page=test 

I need it to work with folders that also exist as usual.

Please tell me how can this be done? I would appreciate any help in resolving this issue.

Also, can someone tell me what the β€œ-MultiViews” bit means in the beginning, since I haven't come across this before?

Thanks.

+4
source share
1 answer

OK first the parameters of MultiViews : according to the Apache manual:

Multiviews

 Content negotiated "MultiViews" are allowed using mod_negotiation. 

In short, if you have a file called foo.html , and if the browser requests http://example.com/foo , then Apache automatically finds and adds the extension and sends foo.html to the browser, rather than throwing 404 . This causes many Rewrite rules to fail, so I turned it off at the beginning in answer to your previous question.

Now, to exclude real directories from rewriting rules, we need an additional condition: RewriteCond %{REQUEST_FILENAME} !-d

To support multiple pretty url schemes:

You have few options here. Please tell me which one you would like to have, and I will provide the rules for this.

Option 1: You have both var name and var value in your url:

www.example.com/page/var1/cat/var2/subcat/var3/subsubcat/var4 OR
www.example.com/othervar1/var1/othervar2/var2/othervar3/var3/othervar4/var4

This gives you maximum flexibility and is very easy to handle in rewriting rules if you are ready to go this route. For this Rewrite parameter, the rules should be written only once, and you will be fine for future changes, since long variables do not change

Option 2: You have a start difference for each circuit. For instance:

www.example.com/handler1/var1/var2/var3/var4 OR
www.example.com/handler2/var1/var2/var3/var4

This gives you a shorter url but does not provide the flexibility like the previous option. But it is also very easy to handle in rewriting rules. However, for this option, rewrite rules must be written for each combination.

So I would recommend option 1 and would be happy to prepare .htaccess for you if you like this option too.

.htaccess file for option 2

 Options +FollowSymlinks -MultiViews RewriteEngine on # 404 handler ErrorDocument 404 /notFound.php RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^handler1/([^/]+)/?$ /?page=$1 [NC,QSA,L] RewriteRule ^handler1/([^/]+)/([^/]+)/?$ /?page=$1&cat=$2 [NC,QSA,L] RewriteRule ^handler1/([^/]+)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3 [NC,QSA,L] RewriteRule ^handler1/([^/]+)/([^/]*)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3subsubcat=$4 [NC,QSA,L] RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^handler2/([^/]+)/?$ /?othervar1=$1 [NC,QSA,L] RewriteRule ^handler2/([^/]+)/([^/]+)/?$ /?othervar1=$1&othervar2=$2 [NC,QSA,L] RewriteRule ^handler2/([^/]+)/([^/]*)/([^/]*)/?$ /?othervar1=$1&othervar2=$2&othervar3=$3 [NC,QSA,L] RewriteRule ^handler2/([^/]+)/([^/]*)/([^/]*)/([^/]*)/?$ /?othervar1=$1&othervar2=$2&othervar3=$3othervar4=$4 [NC,QSA,L] 


.htaccess for option 1

 Options +FollowSymlinks -MultiViews RewriteEngine on # 404 handler ErrorDocument 404 /notFound.php RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/]+)/([^/]+)(/.*)?$ $3?$1=$2 [N,QSA,DPI] RewriteRule ^(/[^/]+|[^/]+/|/?)$ /index.php [L,QSA,DPI] 

Using these rules, the URL http://localhost/n1/v1/n2/v2/n3/v3/n4/v4 will be INTERNALLY redirected to http://localhost/?n4=v4&n3=v3&n2=v2&n1=v1 , processing each pair of URL segments separated by / , like a name-value pair for QUERY_STRING. BUT remember, if the URI does not even have the number of segments, for example: http://localhost/n1/v1/n2/ , then it will be redirected to http://localhost/?n1=v1 , discarding the extra n2.

+2
source

All Articles