0

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.

8
  • You are doing this backwards, quite literally. Commented Dec 3, 2013 at 20:00
  • RewriteRule ^article/([0-9]+)$ /article.php?id=$1 Commented Dec 3, 2013 at 20:01
  • The syntax is RewriteRule (IncomingPath.*) LocalPath?$1 Commented Dec 3, 2013 at 20:01
  • oh... lol my mistake xD but nothing happens :S Commented Dec 3, 2013 at 20:02
  • had my plus sign in the wrong spot, edited. code works :p Commented Dec 3, 2013 at 20:03

2 Answers 2

1

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] 
Sign up to request clarification or add additional context in comments.

18 Comments

Thanks for your help but nothing happens... ? :(
the seconds code works, the first does not work. Still, using the seconds code I get a 404 page...
Yeah, I saw that mistake and I corrected it yet it gives me a 404 error page.
@Th3lmuu90 when you go to /article.php?id=1, do you get a 404?
@Th3lmuu90 or is everything really /articles?id=1 like in your example?
|
0

You are doing this backwards, quite literally.

RewriteRule ^article/([0-9]+)$ /article.php?id=$1 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.