简体   繁体   中英

Apache Rewrite Rules for SEO URL

I am trying to use Apache mod_rewrite to have SEO URLS I have the following:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1

RewriteRule ^([^/]*)\.php$ /index.php?a=$1 [L]
RewriteRule ^([^/]*)/([^/]*)\.php$ /index.php?a=$1&id=$2 [L]

The first part which is redirect to www and https works fine but I need to achieve the following URL rewrites which is not working:

  1. https://www.domain.com/index.php?a=explore to https://www.domain.com/explore/
  2. https://www.domain.com/index.php?a=page&b=about to https://www.domain.com/about/
  3. https://www.domain.com/index.php?a=track&id=9 to https://www.domain.com/track/idno
  4. https://www.domain.com/index.php?a=explore&filter=newtag to https://www.domain.com/explore/newtag/

The following rule should work:

RewriteCond %{THE_REQUEST} ^GET\ /index\.php?a=(explore)(?:&filter=([^\s&]+))? [NC]
RewriteRule ^index\.php$ /%1/%2? [R=301,L,NC]

RewriteCond %{THE_REQUEST} ^GET\ /index\.php?a=(track)&id=(\d+) [NC]
RewriteRule ^index\.php$ /%1/%2/? [R=301,L,NC]

RewriteCond %{THE_REQUEST} ^GET\ /index\.php?a=page&b=about [NC]
RewriteRule ^index\.php$ /about/? [R=301,L,NC]

RewriteRule ^(explore)/(.*)/?$ /index.php?a=$1&filter=$2 [L]
RewriteRule ^(track)/(\d+)/?$ /index.php?a=$1&id=$2 [L]
RewriteRule ^(about)/?$ /index.php?a=page&b=$1 [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM