joelod
joelod

Reputation: 74

Convert URL to SEO Friendly

I have a few questions. In my website, I have, for example, a link like this:

localhost/ecom/index.php?pages=1&title=quem-somos

The index file checks the GET['pages'] for the id, and assign two variables based on info from the db: $static_id, and $static_title.

How do I configure the .htaccess to "convert" this link to this:

localhost/ecom/quem-somos

And when the .htaccess is written, my href should be like this:

localhost/ecom/quem-somos

or, should be like this:

localhost/ecom/index.php?pages=1&title=quem-somos

and the url is converted to the SEO friendly one?

Sorry if there's some stupid questions here, I've read a few blog posts but still can't figure it out.

Upvotes: 1

Views: 429

Answers (1)

zr9
zr9

Reputation: 536

Put this one in your /ecom/ dir

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /ecom/

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteCond %{REQUEST_FILENAME} !\.(css|js|jpeg|jpg|gif|png|bmp)$
  RewriteRule ^([^\/]+)\/([^\/]+)\/?$ index.php?title=$1&pages=$2 [L,B,QSA]
</IfModule>

Maybe RewriteBase is no need, based on your serve configuration, so if some remove.

Or put this one variatn to your root dir

<IfModule mod_rewrite.c>
  RewriteEngine on

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteCond %{REQUEST_FILENAME} !\.(css|js|jpeg|jpg|gif|png|bmp)$
  RewriteRule ^ecom\/([^\/]+)\/([^\/]+)\/?$ /ecom/index.php?title=$1&pages=$2 [L,B,QSA]
</IfModule>

I'm prefer first one. Your urls must be like http://localhost/ecom/some-title/1/
Notice /1/ on the end, it need to pass pages variable

Upvotes: 1

Related Questions