baklap
baklap

Reputation: 2173

Apache2 rewrite strange behaviours

For a newsletter I'm trying to make clean url's for the unsubscribe page

At first I was having this, but it wasn't working:

RewriteEngine On
RewriteBase /
RewriteRule ^/unsubscribe/(.*)$ /unsubscribe.php?email=$1 [NC,L]

Which is very strange in my opinion. So I tried some other methods and ended up with the following:

RewriteEngine On
RewriteBase /
RewriteRule ^/?unsubscribe/?(.*)$ /unsubscribe.php?email=$1 [NC,L]

This, in fact, is working. But it is giving very strange results.

The email parameter value is now:

.php/test

It is driving me nuts because I don't see why it is behaving this way.

Is there anybody who has an idea on what is happening here and how to get it fixed?

I'd rather not end up string replacing the php, there is something wrong here.

Upvotes: 0

Views: 46

Answers (4)

anubhava
anubhava

Reputation: 784898

This rule should work for you:

Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^/?unsubscribe(?:/(.+))?$ /unsubscribe.php?email=$1 [NC,L,QSA]

Problem was in your regex which was matching the pattern after first application and doing the rewrite twice.

Upvotes: 1

baklap
baklap

Reputation: 2173

Ok got it worked out.

It seemed like MultiViews (whatever that may be) was enabled by default.

Adding:

Options -MultiViews

To the htaccess (or apache config) fixed the problem.

Upvotes: 0

Guntram Blohm
Guntram Blohm

Reputation: 9819

Your first Rule doesn't work, because the rule gets what's BEHIND the base in the URL. So if the URL is /unsubscribe/my@email, the RewriteBase (/) gets removed, and the RewriteRule will see unsubscribe/my@email without the leading /.

In your second rule, BOTH /-es are made optional by the ?. So unsubscribe.php/test will find the literal unsubscribe, and take everything behind it - .php/test - and put it in the email parameter. Guess you should use /unsubscribe/test, not /unsubscribe.php/test in your browser when testing.

Upvotes: 1

WarriorTurtle
WarriorTurtle

Reputation: 63

How are you my friend ? :)

Could you try this and let me know if it works? Let's figure this out!

RewriteEngine On

RewriteRule ^unsubscribe/([^/.]+)/?$ unsubscribe.php?email=$1 [L]

Upvotes: 1

Related Questions