homepage Welcome to WebmasterWorld Guest from 54.161.202.106
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
newbie rewrite with conditions (query)
simondev2




msg:1514616
 9:22 pm on Apr 1, 2005 (gmt 0)

HI I got a blog that I am trying to rewrite search engine friendly urls.

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!

 

jdMorgan




msg:1514617
 9:48 pm on Apr 1, 2005 (gmt 0)

Changing the query string links to friendly links is done on the page where those links appear (using php preg_replace is a common method), not by using mod rewrite. In this way, visitors and SE spiders will see them.

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 ^(mpcat)/([0-9]+)/?$ /index.php?$1=$2 [L]

If those are your only three 'fake folders,' then that's the only rule needed to make it work.

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 ^(mpcat)/[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\?(mpcat)=([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]

simondev2




msg:1514618
 10:45 pm on Apr 1, 2005 (gmt 0)

Thanks Jim.I got the first url mod rewrite to work and so now whenever you type in /p/1234 or /m/1234/ or /cat/1234 it will display the correct information.

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\?(mpcat)=([0-9]+)\ HTTP/

Since the index.php is at the root I modified it to :
RewriteCond %{THE_REQUEST} ^\/index\.php\?(mpcat)=([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

simondev2




msg:1514619
 11:05 pm on Apr 1, 2005 (gmt 0)

Here's what I have in my .htaccess file so far (I commented your rewritecond cause it causes a server 500 error for whatever reason):

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]

jdMorgan




msg:1514620
 2:52 am on Apr 2, 2005 (gmt 0)

Sorry, typoed this:

RewriteCond %{THE_REQUEST} ^[A-Z]{[b]3,9[/b]}\ /index\.php\?(mpcat)=([0-9]+)\ HTTP/

I originally had it as {3-9} above which is incorrect. Use {3,9} to allow for three to nine of the preceding character group.

Replace all broken pipe "" characters above with solid pipe characters (usually Shift-\) before use.

Jim

simondev2




msg:1514621
 4:29 pm on Apr 4, 2005 (gmt 0)

Thanks JD. It's working very well now. You rock!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved