Jeff
Jeff

Reputation: 4003

Apache RewriteRule not rewriting as expected

Can you any one see anything wrong with the following apache rewrite rule:

This is in my .htaccess file inside a folder called "text" a subdirectory of localhost/lombardpress I have the following rule

Options +FollowSymlinks
RewriteEngine on
RewriteRule ([^/]+) /textdisplay.php?fs=$1 [NC]

I was expecting this input:

http://localhost/lombardpress-dev/text/lectio1

to rewrite to this:

http://localhost/lombardpress-dev/text/textdisplay?fs=lectio1

But instead I get a 404 error.

The requested URL /textdisplay.php was not found on this server.

It looks to me like the RewriteRule has re-written the address but not as I intended - so there must be something wrong with my regular expression.

Let me know if I can provide further information.

Upvotes: 0

Views: 40

Answers (3)

Jompper
Jompper

Reputation: 1402

Try this

Options +FollowSymlinks
RewriteEngine on
RewriteBase /lombardpress-dev/text/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+) textdisplay.php?fs=$1 [NC]

With that rewrite cond you wont redirect textdisplay.php to itself again. The problem is that [^/]+ matches all but / so it matches even textdisplay.php

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143886

Get rid of the / in front of the target:

RewriteRule ([^/.]+) textdisplay.php?fs=$1 [NC]
# no slash here ----^

Upvotes: 0

anubhava
anubhava

Reputation: 785156

Remove leading slash in target URL:

Try this code:

Options +FollowSymlinks
RewriteEngine on

# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ textdisplay.php?fs=$1 [NC,L,QSA]

Reference: Apache mod_rewrite Introduction

Upvotes: 0

Related Questions