I'm trying to rewrite a url but I can't seem to make it happen.
I just want one working example so I can move on to all pages of my website.
So I have this link:
www.domain.com/article.php?id=1
And I can to change it to:
www.domain.com/article/1/
That's ok for now, later I'll replace the number with the article title.
This is what I have on my .htaccess file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^article\.php\?id=([0-9]+)$ article/$1/
What is wrong it it? I heard that the .htaccess file needs to be in ASCII if using FTP to save in on the server but I don't know how to do it since I created it by myself.
You can't match against the query string in a rewrite rule, you need to match against %{QUERY_STRING}
in a condition:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^article\.php$ article/%1/? [L]
This internally rewrites your request to /article/1/
. The browser will still see the old URL.
What you are more likely looking for is to match against the request:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+article\.php\?id=([0-9]+)
RewriteRule ^ /article/%1/? [L,R=301]
RewriteRule ^article/([0-9]+)/?$ /article.php?id=$1 [L,QSA]
You are doing this backwards, quite literally.
RewriteRule ^article/([0-9]+)$ /article.php?id=$1