Pregunta

I've done the due diligence, spending hours poring through searches and stack QA. No dice. So I finally come here to request help.

  • Apache HTTP Server
  • PHP 5.3

I have dirty urls:

.cc/store/index.php?route=checkout/cart
.cc/store/index.php?route=common/home
.cc/store/index.php?route=product/product&product_id=111

I'd like to clean them so when a user clicks on a dirty link or types a dirty url they get a clean url in the address bar:

.cc/store/cart
.cc/store/home
.cc/store/product/11

Currently I have my htaccess file in:

.cc/store/.htaccess

I know I need in htaccess:

RewriteEngine On

But is this the right path?:

RewriteRule !/index.php?route=(A-Z)/(A-Z)&(*)$ /$2/$3

Q1: Do I need to just edit the htaccess file or will I also have to write some php?

Q2: What htaccess / php code do I write to get the desired clean urls? I want to see clean urls in the address bar of my browser.

Thanks in advance.

¿Fue útil?

Solución 2

This:

RewriteRule !/index.php?route=(A-Z)/(A-Z)&(*)$ /$2/$3

Isn't what you want. You're going to need 2 types of rules, ones that externally redirect the browser to the URL that you want to see, then ones that internally rewrite to the URL that your system can understand (the "dirty" ones). So something like:

RewriteEngine On
RewriteBase /store/

RewriteCond %{THE_REQUEST} \ /+store/index\.php\?route=checkout/cart
RewriteRule ^ /store/cart? [L,R]

RewriteCond %{THE_REQUEST} \ /+store/index\.php\?route=common/home
RewriteRule ^ /store/home? [L,R]

RewriteCond %{THE_REQUEST} \ /+store/index\.php\?route=product/product&product_id=([^&\ ]+)
RewriteRule ^ /store/product/%1? [L,R]

RewriteRule ^cart$ index.php?route=checkout/cart [L,QSA]
RewriteRule ^home$ index.php?route=common/home [L,QSA]
RewriteRule ^product/(.+)$ index.php?route=product/product&product_id=$1 [L,QSA]

You can't make it "wildcard" like matching because you're changing something like "X/Y" to just "Y", which means when you internally rewrite it back, the "X" part is lost forever.

Otros consejos

Whatever is generating those URLs, be it PHP or simple HTML, will need to be updated to contain the new URLs. mod_rewrite simply takes the "Clean" url and translates it into the original "dirty" one so your original code can still function, with the same parameters.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top