carrmx carrmx - 4 months ago 25
Apache Configuration Question

.htaccess RewriteRule is not redirecting the page

im trying to redirect a link like this:

to its equivalent:

and so in .htaccess ive written:

RewriteRule ^/strangers/([0-9]+)$ /strangers/strangers.php?img=$1 [R=301]

but nothing is happening. i can't see what's wrong--am i writing them in the wrong order? here is my full .htaccess just in case there's something conflicting that i'm not seeing:

# use php in html files i think
AddHandler application/x-httpd-php5 .html

DirectoryIndex home.html

RewriteEngine On

#removes html extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC]

RewriteRule ^/strangers/([0-9]+)$ /strangers/strangers.php?img=$1 [R=301]

by the way, which link will actually display in the browser?


Your first rule is going to match and rewrite to strangers/2.html, you'll need a condition to exclude the strangers URI from the first rule:

RewriteBase /
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/strangers/.*$
RewriteRule ^([^\.]+)$ $1.html [NC,L]

RewriteRule ^strangers/([0-9]+)$ /strangers/strangers.php?img=$1 [L]

I removed the 301 flag from the last rule. I'm assuming you don't really want to redirect to /strangers/strangers.php?img=2, but rather show strangers/2 in the browser url bar while apache executes /strangers/strangers.php?img=$1 under the covers.