.htaccess rewrite rules for multiple parameters

Rewriting the following 3 urls

http://example.com/index.php?page=search

in http://example.com/search

http://example.com/index.php?page=profile&value=myname

in http://example.com/myname

http://example.com/index.php?page=stats&type=daily

in http://example.com/stats/daily

The current .htaccess is written like this:

Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&value=([^\s&]+) [NC]
RewriteRule ^ index/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&type=([^\s&]+) [NC]
RewriteRule ^ index/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ index/%1/? [R=302,L,NE]

RewriteRule ^index/([^/]+)/([^/]+)/?$ /index.php?page=$1&value=$2 [NC,L,QSA]
RewriteRule ^index/([^/]+)/([^/]+)/?$ /index.php?page=$1&type=$2 [NC,L,QSA]
RewriteRule ^index/([^/]+)/?$ /index.php?page=$1 [NC,L,QSA]

      

I need to remove / index / from the first and third urls and / index / profile / from the second.

All comments and suggestions are greatly appreciated.

+3


source to share


2 answers


There are presumably 3 exceptions (special cases) to the current rules, so the existing rules should not change. (?)

Add the following "special cases" right after your directive, RewriteBase

before your existing rules:

RewriteCond %{THE_REQUEST} /index\.php\?page=(search)
RewriteRule ^ /%1? [R=302,L]

RewriteCond %{THE_REQUEST} /index\.php\?page=profile&value=(myname)
RewriteRule ^ /%1? [R=302,L]

RewriteCond %{THE_REQUEST} /index\.php\?page=(stats)&type=(daily)
RewriteRule ^ /%1/%2? [R=302,L]

      



If you need to internally rewrite those specific URLs back to "real" parameterized versions, then at the end of your .htaccess file:

RewriteRule ^(search)$ /index.php?page=$1 [L,QSA]
RewriteRule ^(myname)$ /index.php?page=profile&value=$1 [L,QSA]
RewriteRule ^(stats)/(daily)$ /index.php?page=$1&type=$2 [L,QSA]

      

(I removed the flag NC

- add it back if you really need it.)

+2


source


You cannot have both a search and a profile of the same regex pattern. For example, if someone asks:

http://example.com/index/foo

      

Are they trying to navigate to a page called "foo" or to the profile of a user named "foo"? You need to add something that separates the two, maybe something like:



Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /index\.php\?page=profile&value=([^\s&]+) [NC]
RewriteRule ^ profile/%1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&type=([^\s&]+) [NC]
RewriteRule ^ page/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)(\ |$) [NC]
RewriteRule ^ page/%1? [R=302,L,NE]

RewriteRule ^profile/([^/]+)/?$ /index.php?page=profile&value=$1 [NC,L,QSA]
RewriteRule ^page/([^/]+)/([^/]+)/?$ /index.php?page=$1&type=$2 [NC,L,QSA]
RewriteRule ^page/([^/]+)/?$ /index.php?page=$1 [NC,L,QSA]

      

This makes your urls look like:

http://example.com/profile/myname

http://example.com/page/search

http://example.com/page/stats/daily

      

+2


source







All Articles