-1

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.

openyk
  • 65
  • 2
  • 9

2 Answers2

1

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.

Jessica
  • 7,075
  • 28
  • 39
  • 1
    Actually that is not true: you _can_ use the rewriting module for this purpose by making external redirects. Though I do agree that _most likely_ is not looking for such a solution. – arkascha May 05 '14 at 18:19
  • Ah so an alternative method is to edit the link generator (in html/php) to make clean urls, then the user would see those clean urls while htaccess would transform the clean url to the dirty one for the server the process (ie. server outputs clean url, user sees clean url, server dirties url, server processes dirty url). Cool! – openyk May 05 '14 at 20:37
0

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.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • It worked! I'll extend this syntax to the other pages for url cleaning. Also the external/internal redirect concept makes sense! Thanks so much! – openyk May 05 '14 at 20:25