Rewriting Get Variables in .htaccess

I have a url similiar to:

http://www.domain.com/article.php?id=1

I want to rewrite the URL to:

http://www.domain.com/article-title

Any ideas on how I can process the article title into .htaccess and rewrite?

You can’t do this with mod_rewrite, since apache doesn’t know what title corresponds with id=1
So you could either look at the Redirect directive to redirect a few URLs manually, or if there are too much to do manually and you have access to the server config (httpd.conf) you could take a look at RewriteMap

Can you process the title in the get variable? Such as this:

http://www.domain.com/article.php?title=article-title

Rewrite the URL to:

http://www.domain.com/article-title

I want to rewrite the URL to:

http://www.domain.com/article-title

Yes that is possible :slight_smile:


Options +SymLinks
RewriteEngine On
RewriteRule (.*) article.php?article=$1 [L]

Note that that will match anything and everything, so you’d probably better off not firing the rule for existing directories and files:


Options +SymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) article.php?article=$1 [L]

I’ve tried that as well as a few different options. Unfortuantely, I’m not getting it’s not redirecting. I verified that mod_rewrite.c is on in the server.

I was able to get the following to work:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) article.php?article=$1 [L]

However, I’m trying to add another line using the same rewrite rule with the same code:

RewriteRule (.*) article-spanish.php?article=$1 [L]

It is conflicting with the orginal RewriteRule and won’t distinguish the second rewrite rule as something different.

What is the best way to distinguish it?

I usually prefix the URL with a 2 char language code like /es/ for spanish, /en/ for english, /fr/ for france, etc.
And I leave the main language without a prefix. So for example:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^es/(.*) article-spanish.php?article=$1 [L] 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) article-spanish.php?article=$1 [L] 

BTW, try to stay clear from :redhot: (.*) :redhot: but think what characters you actually want to accept and modify the code for that. For example to accept letters, digits and dashes, use


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^es/([a-zA-Z0-9-]+)$ article-spanish.php?article=$1 [L] 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)$ article-spanish.php?article=$1 [L] 

It’s a lot more clean and will never produce any unpredicted results since you’ve specified exactly what characters are allowed in an URL and which aren’t.