RewriteRule Not Working

How can I implement RewriteRule on Apache? I use EasyPHP 5.3 and I’ve followed this tutorial so mod_rewrite is enabled but I can’t reformat the URL


to a friednly one. I have created code for .htaccess file using this generator so I got:

Options +FollowSymLinks
RewriteEngine on
RewriteRule test(.*)\\.htm$ /index.php?id_al=$1

The problem: nothing happens if I access [i]http://localhost/index.php?id_al=7[/i]

Am I missing something?


First, WELCOME to SitePoint’s Apache forum!

WHY ask at SitePoint when you’re using an article - even worse, using automated code generators - from other websites? I disagree with a couple of things in that “article” and loathe “generators” but let’s get to your problem.

If that’s the URI you’re attempting to redirect, WHERE are you trying to redirect it TO?

First thing that you MUST learn is that it’s YOUR job to link TO the "new format, i.e., mod_rewrite’s job is to convert from your NEW (SEO) format link TO the URI that Apache can serve. (Okay, a member a year and a half ago asked for help to redirect TO the new format then back to something Apache can serve but let’s learn to walk before we run, eh?)

I try to emphasize “specificity:” What is the format of the link which you are trying to redirect and what is the link you need to redirect that to? That will get you thinking about your NEW format link and whether it’s too vague (will redirect too much) and what characters you need to match.

Where you went wrong was trying to match the query string with your RewriteRule - it can’t be done (RewriteRules can only examine the {REQUEST_URI} variable; the {QUERY_STRING} can only be examined by a RewriteCond).

[COLOR="Gray"]Options +FollowSymLinks[/COLOR]
# That's probably already in your server's configuration file
RewriteEngine on
RewriteRule test(.*)\\.htm$ /index.php?id_al=$1
# No Last flag?
# test.htm to test{lotza_ garbage}.htm gets
# redirected to index.php with a query string of
# id_al={lotza_garbage}? 
# Seriously?  You don't know what your {lotza_garbage}
# consists of?
# THEN, using the leading / in the redirection forces Apache
# to look to the server's root directory BEFORE looking to
# your DocumentRoot for index.php.

The (.*) deserves special comment, i.e., a “standard rant:”

[standard rant #1][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is “greedy.” Unless you provide an “exit” from your redirection, you will ALWAYS end up in a loop![/indent][/standard rant #1]

In other words, ONLY use (.*) if you NEED to match NOTHING or EVERYTHING. This is the most abuse code because newbies do not understand regex. LEARN some regex (and the tutorial linked in my signature has helped many members with that simple task as well as offering quite a few sample code blocks to resolve specific problems).



Thanks for your welcome note!

I don’t want to redirect my URL just trying to make it friendly for search engines (let’s say I will replace id_al to news).

http://localhost/index/news/7 or http://localhost/news/7

I tried your code for htaccess and nothing changes…


Sorry to disillusion you but “my code” was YOUR code (with my comments). However, that code WILL work IF - and ONLY if - your test URI is testX.htm where X is nothing or … ANYYTHING. Please READ the following:





I still don’t get it. Are you saying that I can’t “translate” my link?

All kind of tutorials on the net suggest that can be done. See this one which explains how to “translate”


I need to do the same thing:




Is it possible? It should be an easy answer, isn’t it?


RewriteRule ^news/(\d+)/?$ /index.php?news=$1 [L]


apo is trying to get mod_rewrite to reinvent his links for him rather than acting like a webmaster and creating them him/herself. FWIW, your code will work but it will suffer the “Missing Support Files” problem because of the optional trailing /.


If you don’t get it, please READ the first of the quotes again. I can assure you that you do not want any server to GUESS what format you want your links in and change them willy-nilly. THAT IS YOUR JOB (if you want SEO/user-friendly links).

I don’t need to read any of the “silly” mod_rewrite tutorials running wild around the Internet. If you want to “translate” a link which Apache can serve to one it can’t, that’s a trivial task - and will result in abundant 404 errors until you can “translate” back to something Apache CAN serve (without creating a LOOP while you’re at it). If you’d read the tutorial, you would KNOW that there is a section on doing just exactly that! Did you bother?

Yes, it’s SIMPLE:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^news=(\\d+)$
RewriteRule ^index\\.php$ news/%1 [R=301,L]

That will do EXACTLY what you’re asking for and you can verify that with the URL in your browser’s location box. After that, you’ll need to actually READ the responses you’ve been receiving.

WARNING TO OTHERS: Do NOT use this code as it will result in a 404 code redirection to the designated 404 script.



Oh, I see, didn’t realize he wanted an external redirect.


I don’t think he know what he wants except to reformat his links. I believe he wants the internal link but wants Apache to “read his mind” and redirect to some new format but won’t tell it what that new format is. Maybe we’ll see when he attempts his http://localhost/index.php?news=7 and sees that it will redirect to news/7 as he requested but gets a 404 for his trouble.



It didn’t do any change on my local URL

I got an error. URL generated:


I don’t want an external redirect; I just need to have nice formated URL which will work (no 404 errors) as I wouldn’t have this rewrite code in place.


The code provided by Ijj earlier was spot on (well except for the trailing / as dklynn pointed out):

RewriteEngine On
RewriteRule ^news/(\\d+)$ /index.php?news=$1 [L]

Note that with this in place it is up to YOU to change the links in your HTML.

That is, if you had /index.php?news=1 as a link in your HTML, you should change that to /news/1

Does that make it clear?

Note that when you do that you might run into the trouble of missing support files (js and css files not loading). To overcome that either:

  1. create absolute URLs to the support files (/js/my-js.js instead of js/my-js.js)
  2. insert <base href=“/” /> in the head of the HTML

I think all this was just that it wasn’t explained very well that
mod_rewrite does not change urls!

It does not turn index.php?ugly=page&id=1234 to /page/1234

A lot of those tutorials don’t make that clear. Since /page/1234 does not exist, Apache can’t do anything.

Scallio made it somewhat clearer that the URLs that are linking to index.php?ugly=page&id=1234 have to be changed in the HTML source to /page/1234 and then Apache’s job is to take that “/page/1234” and be able to give the user the page found at “index.php?ugly=page&id=1234” while not showing them that ugly version in their address bars.

Anyone who happens to find an old link to that page which is still in the ugly format will simply also get that page, because the ugly version is not matched by the regex and Apache doesn’t do anything special but just serve the page like it always had.

Does this make more sense? I didn’t get this right away either, because everywhere you see “use mod_rewrite to make your URLs pretty” which is bull. Those tuts SHOULD be saying “use mod_rewrite to make your pre-made pretty URLs in your links actually go to a real, existing page you have”.

Thanks, Mallory, you’re not the THIRD person to say this so I hope it finally gets through to apo!

BTW, I DO have that info in my tut, too, but that’s gone for naught here, too.

Thanks for trying.