Apache2 rewrite strange behaviours - apache

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.

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]

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.

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.

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.

Related

Simple rewrite not working (.htaccess)

I'm trying to work out a bigger problem, which I believe lies in my .htaccess file. So I stripped it down to the following for testing purposes:
RewriteEngine on
RewriteRule ^sandbox/htaccess/one.php$ sandbox/htaccess/two.php
I'm simply trying to get requests for
http://example.com/sandbox/htaccess/one.php
to go to
http://example.com/sandbox/htaccess/two.php
However, this is not working. I simply see one.php.
I can confirm that .htaccess file is being read.
What am I missing to get this simple example working?
Try this, it will work
RewriteEngine on
RewriteBase /
RewriteRule ^sandbox/htaccess/one\.php$ /sandbox/htaccess/two.php? [L,R=301]

Regex of a RewriteRule isn't working

I'm actually using for the first time the RewriteRule command in a .htaccess, and I'm stuck...
What I'm trying to do is to make such redirection:
localhost/tchat/test_character.swf -> localhost/tchat/characters/test_character.swf
I'm using this current .htacces, as I don't know really where I was supposed to place it, I placed it in / and a copy in /tchat.
RewriteEngine On
RewriteRule ^tchat/([^_/])_character\.swf$ /tchat/character/$1_character.swf [R=301]
But when I try to access the /tchat/test_character.swf I just get a 404...
I think the problem is in my regex but I just can't figure where inside the regex. I've already tried to add a RewriteBase / after the RewriteEngine On, and I also tried to add a / beetwen the ^ and tchat. But it doesn't worked.
So I'm really curious to know where the regex fail...
Thank's a lot!
Ps: Please excuse my English, It's not my native language :/
Finally, it worked by changing the htaccess to:
RewriteEngine On
RewriteBase /tchat
RewriteRule ^([^_/]+)_character\.swf$ /tchat/character/$1_character.swf [R=301]

.htaccess RewriteRule using # sign?

I am trying to have a URL that would look like:
mysite.com/#username
Which would route to something like:
subdomain.mysite.com/user/#username
I'm terrible with RewriteRules and have been struggling to try to get this to work. Some of the things I have tried are:
RewriteRule subdomain.mysite.com/user/(.*) mysite.com/$1 [R=302,NC,L]
RewriteRule ^(.#)(.*) subdomain.mysite.com/user/$1 [R=302,NC]
RewriteRule ^(.#)([A-Za-z0-9_]+) mysite.com/user/$1 [R=302,NC]
I realize those probably make absolutely no sense. Every time I try to get my head around how the routing works, I get turned around and start writing crap like you see above.
Any pointers would be appreciated. Thanks.
You can't route to a subdomain (using htaccess anyway) but you can redirect there.
RewriteEngine On
RewriteBase /
RewriteRule #(.*) http://subdomain.mysite.com/user/#$1 [L,R=301]
The # symbol should be okay https://stackoverflow.com/a/1547940/763468

Using .htaccess Mod_rewrite to hide .php extension only working for missing pages

Please excuse me as I'm pretty new to using .htaccess, and am having a few issues with it as the setup I need is, despite being simple, seemingly rare.
Basically, I'm working on a secondary domain trying to hide .php extensions from pages, but whilst also redirecting requests for nonexistent pages to a custom search page. The overall desired effect is:
realpage => realpage.php
and
falsepage => search.php?q=falsepage
So far, the code I have (see below) seems to apply the latter correctly, but rather than returning 'realpage.php' for 'domain.com/realpage' it returns a 404 error.
I've found variations which also move realpage to 'search.php?q=missing' in case that gives any indication what might be going wrong!
My .htaccess file at the moment:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ $1.php [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)\.php$ search.php?q=$1 [L]
Thanks for any help you can offer! It's very much appreciated!
I think for your first rule, you want something along the lines of:
RewriteBase /
RewriteRule ^([^.]+)$ $1.php [L]
I ran that through the rewrite rule tester, and it will direct 'realpage' to 'realpage.php'. If that doesn't work, I would suggest disabling your second rule, making sure the first one works well, then adding the check for missing files in later.

What's wrong with this mod_rewrite rule?

Can anyone spot what is wrong with this URL rewrite? I can't get it to pass anything to GET (the script loads, but no parameters are passed):
RewriteRule ^archive(/(.*))?$ archive.php?action=$1 [QSA,L]
I want to map "archive/browse/" to "archive.php?action=browse".
You can get some conflicts when MultiViews is enabled. If it’s enabled, Apache first tries to find a file with a similar name to map the request to before passing it down to mod_rewrite. So a request of /archive/browse/ ends in /archive.php/browse/ before mod_rewrite can map it to your /archive.php?action=browse.
Try to disable it with:
Options -MultiViews
RewriteEngine On
RewriteRule ^archive/(.*)$ archive.php?action=$1 [QSA,L]
Would rewrite anything after /archive/ to archive.php?action=test/dir/path
RewriteRule ^archive/([^/]*) archive.php?action=$1 [QSA,L]