how to solve this

New to mod_rewrite? This is a good place to start.

how to solve this

Postby alanshen209 » Tue Oct 27, 2009 8:03 am

Hi,

I have no idea about how the do the rewite. and I need to do the following rewite (from left to right):

dddd-k.html -> index.php?k=dddd
ads.html -> index.php?listtype=ads
ads-33.html -> index.php?listtype=ads&p=33
cat.html -> index.php?listtype=cat
cat-33.html -> index.php?listtype=cat&p=33
link-20.html -> index.php?listtype=link&c=20
link-20-2.html -> index.php?listtype=link&c=20&p=2
some_text-link-20-2.html -> index.php?listtype=link&c=20&p=2&t=some_text

Could anyone give me some help?

Thanks
alanshen209
 
Posts: 3
Joined: Tue Oct 27, 2009 8:01 am

Postby richardk » Tue Oct 27, 2009 11:52 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# ads.html -> index.php?listtype=ads
# cat.html -> index.php?listtype=cat
RewriteRule ^(ads|cat)\.html$ /index.php?listtype=$1 [QSA,L]

# ads-33.html -> index.php?listtype=ads&p=33
# cat-33.html -> index.php?listtype=cat&p=33
RewriteRule ^(ads|cat)-([0-9]+)\.html$ /index.php?listtype=$1&p=$2 [QSA,L]

# link-20.html -> index.php?listtype=link&c=20
RewriteRule ^link-([0-9]+)\.html$ /index.php?listtype=link&c=$1 [QSA,L]

# link-20-2.html -> index.php?listtype=link&c=20&p=2
RewriteRule ^link-([0-9]+)-([0-9]+)\.html$ /index.php?listtype=link&c=$1&p=$2 [QSA,L]

# some_text-link-20-2.html -> index.php?listtype=link&c=20&p=2&t=some_text
RewriteRule ^([^/]+)-link-([0-9]+)-([0-9]+)\.html$ /index.php?listtype=link&t=$1&c=$2&p=$3 [QSA,L]

# dddd-k.html -> index.php?k=dddd
RewriteRule ^([^-/]+)-([^-/]+)\.html$ /index.php?$2=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alanshen209 » Tue Oct 27, 2009 12:53 pm

Hi,

Thanks for your reply.

I have a lot of sites configured like:
<VirtualHost my_ip>
ServerName www.the_site.com
ServerAlias the_site.com
DocumentRoot /var/www/html/the_site.com
<Directory /var/www/html/the_site.com>
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>

Your code works when I create a .htaccess file in the_site.com folder.

but I want the same code works for all the similar sites, I tried to put the same .htaccess file at the folder /var/www/html and remove the one at the sub foloer, it doesn't work. I also tried to put your script in the httpd.conf file. it doesn't work either.

Any idea about how to fix this?

Thanks a million times!!
alanshen209
 
Posts: 3
Joined: Tue Oct 27, 2009 8:01 am

Postby richardk » Wed Oct 28, 2009 11:10 am

Try adding
Code: Select all
<Directory /var/www/html/*>
  Options +FollowSymLinks

  RewriteEngine On

  # ads.html -> index.php?listtype=ads
  # cat.html -> index.php?listtype=cat
  RewriteRule ^(ads|cat)\.html$ /index.php?listtype=$1 [QSA,L]

  # ads-33.html -> index.php?listtype=ads&p=33
  # cat-33.html -> index.php?listtype=cat&p=33
  RewriteRule ^(ads|cat)-([0-9]+)\.html$ /index.php?listtype=$1&p=$2 [QSA,L]

  # link-20.html -> index.php?listtype=link&c=20
  RewriteRule ^link-([0-9]+)\.html$ /index.php?listtype=link&c=$1 [QSA,L]

  # link-20-2.html -> index.php?listtype=link&c=20&p=2
  RewriteRule ^link-([0-9]+)-([0-9]+)\.html$ /index.php?listtype=link&c=$1&p=$2 [QSA,L]

  # some_text-link-20-2.html -> index.php?listtype=link&c=20&p=2&t=some_text
  RewriteRule ^([^/]+)-link-([0-9]+)-([0-9]+)\.html$ /index.php?listtype=link&t=$1&c=$2&p=$3 [QSA,L]

  # dddd-k.html -> index.php?k=dddd
  RewriteRule ^([^-/]+)-([^-/]+)\.html$ /index.php?$2=$1 [QSA,L]
</Directory>

after all the <VirtualHost>s.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alanshen209 » Mon Nov 16, 2009 12:24 pm

how can I change the file seperator from - to a folder structure, like:

from:
http://www.domain.com/k/kw1/tw2.htm
to:
http://www.domain.com/index.php?k=kw1&t=kw2

I tried to replace - with /, but it doesnt work.

Thanks
alanshen209
 
Posts: 3
Joined: Tue Oct 27, 2009 8:01 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 30 guests

cron