Forum Moderators: phranque
I have 3 different parameters for a script:
www.domain.com/index.php?p=1234
www.domain.com/index.php?m=1234
www.domain.com/index.php?cat=1234
I've written rules so it recognizes the url:
[domain.com...] to be served by [domain.com...]
Just trying to figure out how to flatten the files according to parameter variables using a condition.
i.e.
www.domain.com/index.php?p=1234 => www.domain.com/p/1234
www.domain.com/index.php?m=1234 => www.domain.com/m/1234
www.domain.com/index.php?cat=1234 => www.domain.com/cat/1234
Here's what I have so far in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
#case parameter 'p' post
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^$ /p/%1/ [R]
#case parameter 'm' post
RewriteCond %{QUERY_STRING} ^m=([0-9]+)
RewriteRule ^$ /m/%1/ [R]
#case parameter 'cat' post
RewriteCond %{QUERY_STRING} ^cat=([0-9]+)
RewriteRule ^$ /cat/%1/ [R]
#rewrite the individual fake folders
RewriteRule ^m/([0-9]+) /index.php?m=$1 [L]
RewriteRule ^p/([0-9]+) /index.php?p=$1 [L]
RewriteRule ^cat/([0-9]+) /index.php?cat=$1 [L]
I have also tried the following:
Options +FollowSymLinks
RewriteEngine on
RewriteRule index/(.*)/(.*)/$ /index.php?$1=$2
But neither work for whatever reason.
Thanks!
Once one of those friendly links is requested by a client, your server can then rewrite it to the form needed to call your script:
#rewrite the m, p, and cat fake folders
RewriteRule ^(m¦p¦cat)/([0-9]+)/?$ /index.php?$1=$2 [L]
If your script has already been modified to extract the correct parameters from the friendly URL, then you don't need to rewrite the query string at all; Just rewrite requests for any of those three folders to the script:
#rewrite the m, p, and cat fake folders
RewriteRule ^(m¦p¦cat)/[0-9]+/?$ /index.php [L]
After getting this working, what usually happens is we get another question: "How do I remove those old query-string URLs from the search engine indices? The answer is to use an external redirect, looking only at the original (non-rewritten) URL received in the client's HTTP request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?(m¦p¦cat)=([0-9]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%1/%2/ [R=301,L]
Because this ruleset looks only at the client browser/spider's request, and not at any previously-rewritten URLs, the two rules won't interefere with each other and create a loop; They can appear in .htaccess in any order.
BTW, an example of the form of %{THE_REQUEST} received from a browser is "GET /index.php?p=1234 HTTP/1.1"
You can see these requests in most server's raw access logs, which are built from the same server variables that mod_rewrite can test.
Replace all broken pipe "¦" characters above with solid pipe characters (usually Shift-\) before use.
Jim
[edited by: jdMorgan at 6:25 pm (utc) on April 2, 2005]
However I am having problems with the conditions:
Getting this error in my error log:
RewriteCond: cannot compile regular expression '^[A-Z]{3-9}\\ /index\\.php\\?(m¦p¦cat)=([0-9]+)\\ HTTP/'\n
For this line:
RewriteCond %{THE_REQUEST} ^[A-Z]{3-9}\ /index\.php\?(m¦p¦cat)=([0-9]+)\ HTTP/
Since the index.php is at the root I modified it to :
RewriteCond %{THE_REQUEST} ^\/index\.php\?(m¦p¦cat)=([0-9]+)\ HTTP/
But it doesn't seem to forward correcly:
For example: www.domain.com/index.php?p=333 doesn't get rewritten to www.domain.com/p/333
Thanks
Options +FollowSymLinks
RewriteEngine on
#rewrite the m, p, and cat fake folders
RewriteRule ^(m¦p¦cat)/([0-9]+)/?$ /index.php?$1=$2 [L]
#fake folders
#RewriteCond %{THE_REQUEST} ^[A-Z]{3-9}\ /index\.php\?(m¦p¦cat)=([0-9]+)\ HTTP/
RewriteCond %{THE_REQUEST} ^\/index\.php\?(m¦p¦cat)=([0-9]+)
RewriteRule ^index\.php$ [plateoftheday.com...] [R=301,L]
#default for a numbered directory is posting.
RewriteRule ^([0-9]+)$ /$1/ [R=301]
RewriteRule ^([0-9]+)/$ /index.php?p=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{[b]3,9[/b]}\ /index\.php\?(m¦p¦cat)=([0-9]+)\ HTTP/
Replace all broken pipe "¦" characters above with solid pipe characters (usually Shift-\) before use.
Jim