What's wrong with this mod_rewrite rule? - apache

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]

Related

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.
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.

mod_rewrite 404 error trying to rewrite URL

I have website that is using unfriendly URLs and I want to change them using mod_rewrite.
I have a URL like this:
http://www.website.nl/?p=2
and I want it to be
http://www.website.nl/about-us
When I use this on my local server it works correct but on the live webserver it doesnt
I use this code in my .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^about-us$ /?p=2 [L]
I already checked if mod_rewrite is enabled and in my apache2handler it is enabled so it should work shouldn't it?
What am I missing or doing wrong?
You may be missing the leading / (forward slash) in your pattern, try:
RewriteRule ^/about-us$ /?p=2 [L]

Apache Rewrite with Multiple Parameters

I need to rewrite the following types of URLs:
http://www.gocruise.co.uk/fusion/detailline3.pl?lineid=13&sid=6924&ccid=Fred+Olsen
into:
http://www.gocruise.co.uk/fred-olsen
using an Apache RewriteRule. I have been trying to get to grips with these rewrites as quick as i can but have run out of time. Any help will be much appreciated.
(the main bit im struggling with is how to manage the multiple parameters)
You probably want it the other way round: A request of /fred-olsen comes in, and you want to redirect the user to the longer URL. This is pretty simple:
# in the Server Configuration or VHost Configuration:
RewriteRule ^/fred-olsen$ /fusion/detailline3.pl?lineid=13&sid=6924&ccid=Fred+Olsen [R=301,L]
or:
# in the .htaccess file of the DocumentRoot
RewriteRule ^fred-olsen$ fusion/detailline3.pl?lineid=13&sid=6924&ccid=Fred+Olsen [R=301,L]
Unless lineid and sid are always going to be the same values, or they're not important to the code, I don't think it's going to work. Typically, url rewrites include all the parameters, so you'd end up with something like http://www.gocruise.co.uk/13/6924/fred-olsen
Alright, the following rules should do what you're asking. The other two parameters will be sent in the querystring. The only other way would be to add them into the URL, which it doesn't seem like that is what you want.
# http://www.gocruise.co.uk/fusion/detailline3.pl?lineid=13&sid=6924&ccid=Fred+Olsen
# redirects to ->
# http://www.gocruise.co.uk/Fred-Olsen?lineid=13&sid=6924
RewriteEngine On
RewriteCond %{REQUEST_URI} /fusion/detailline3.pl
RewriteCond %{QUERY_STRING} (.*)&ccid=(\w*)\+(\w*)$
RewriteRule (.*) /%2-%3?%1 [L,R=301]

Rewrite rules doesn't work apache 1.3

I'm using a couple of rewrite directives that always works before on apache2 but now trying a new shared hosting and the rewrite rules do not seem to get applied.
I've reduced the .htaccess files to the following essential rules:
RewriteEngine On
Rewritebase /demo/
RewriteRule ^(.*)$ index.php/$1 [L]
As you can see, i want to rewrite every request to my index.php file in the demo folder from root.
So everything like http://www.example.com/demo/albums/show/1 should be processed by http://www.example.com/demo/index.php for a standard MVC setup. (I'm using CodeIgniter btw)
The directives above results in a 500 error, so i thought maybe because of some possible syntax differences between 1.3 and 2.x.
After some trail and error editing, i've found the rewrite rule itself to be at fault but i really don't understand why.
Any ideas to why my rewrite rule doesn't work? it did before on lots of different servers.
Suggestions how to fix it?
Note: mod_rewrite does work, i've written a small test to be sure.
In your position, I'd probably look in the Apache error log first, then would try to eliminate one moving part by doing
RewriteEngine On
RewriteRule ^/demo/index.php$ /demo/index.php [L]
RewriteRule ^/demo/(.*)$ /demo/index.php/$1 [L]
If that worked, I'd try reintroducing RewriteBase.
Did you set:
Options +FollowSymLinks
… before the rewrite rules? If FollowSymLinks is disabled mod_rewrite won’t work.
It may be that you’re running into an infinite recursion since index.php/… is also matched by ^(.*)$. So try to exclude your target:
RewriteCond $1 !^index\.php/
RewriteRule ^(.*)$ index.php/$1 [L]

Is it possible to alias a filename on an apache webserver?

I would like to make e.g. www.address.com/u.exe equal to www.address.com/serverfile.php or pl?
Is it possible?
So if someone types www.address.com/u.exe should get servefile.php...
Thanks for showing the right direction..
This seems to work. RewriteEngine on also had to be added.
I had to change .htaccess file
RewriteEngine on
RewriteRule ^u\.exe$ serverfile.php
Yes. That's what the mod_alias Apache module does for you: http://httpd.apache.org/docs/2.2/mod/mod_alias.html#alias
Yes, it's possible with mod_rewrite like below.
RewriteRule ^/u.exe$ /serverfile.php [L]
Or below if you want to display serverfile.php (via a redirect).
RewriteRule ^/u.exe$ /serverfile.php [RL]