[BlueOnyx:10282] Re: RewriteRule problem

Michael Stauber mstauber at blueonyx.it
Sat Apr 21 20:33:08 -05 2012


Hi DD,

> In my /etc/httpd/conf/vhosts/siteXX.include file I have:
> 
>   RewriteEngine On
>   RewriteRule ^/([a-z\-]+).html$	/index.php?stub=$1
> 
> The idea is that a URL such as /hello-world.html becomes
> /index.php?stub=hello-world and the content can be pulled from a DB.
> 
> I'm getting a 404 for known values of stub in the "blah.html" format URL,
> but if I try it directly via "index.php?stub=blah" it's fine.

Your regular expression ^/([a-z\-]+).html$ is a bit flawed, sorry.

This works better:

^([a-zA-Z0-9_-]+).html$

^ 	= Match beginning of the line
a-z 	= lower case letters
A-Z  = upper case letters
0-9   = numbers
_	= underscores
-	= minus sign
.html$ = must have .html at the very end of the input.

Part of the problem there is that the RewriteRule's in siteXX.include files 
trigger only after the "file not found" rules. 

To counter that, put rewrite rules like this in a .htaccess file in your /web 
folder.

> I've used AllowOverride to move the rule to a .htaccess file in the site's
> /web/ folder. I've deleted the site and recreated it, restarted Apache and
> eventually the server too, all to no avail.

I see. I just tried it myself and here is what I got to work in an .htaccess 
file:

Example 1:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+).html$     /index.php?stub=$1

When you access http://www.site.com/hello.html, the URL in the browser will 
not change. But the content is served off index.php?stub=hello

Example 2:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+).html$ http://www.site.com/index.php?stub=$1 
[L,R=301]

When you access http://www.site.com/hello.html, the URL in the browser will 
change to http://www.site.com/index.php?stub=hello and the page is served from 
there.

-- 
With best regards

Michael Stauber



More information about the Blueonyx mailing list