Apache Rewrite with Multiple Parameters - apache

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]

Related

.htaccess redirect according to PHP parameters

I'm trying to build some redirections with my .htaccess file to deal with some old forum url. I want the redirections to be made according to the PHP parameters (the topic ID).
For instance, something like
RewriteEngine On
RedirectPermanent /forum/viewtopic.php?t=123 /page1.html
RedirectPermanent /forum/viewtopic.php?t=345 /page7.html
RedirectPermanent /forum/viewtopic.php?t=89 /page3.html
The old and new URL are not related to each other (no PHP parameter has to be kept or something). I want to decide manually in my .htaccess file what to do for each topic ID.
But I can't manage to do that so easily. I tried many things, but nothing works.
Is this possible ? Any idea ?
Thanks a lot !
Edit : additional question : I want to add a global redirection of all the folder /forum to the root of the site ("/"). I guess I can place it after the others, so if no other rule is trigered, this one will be trigered.
I'm trying some things like
RewriteRule ^forum /? [L,R=301]
But everything I have tried so far redirects me to the "page1.html" (my first rule). Any idea why ? Thanks a lot !
You can't match against the query string using mod_alias's Redirect, RedirectMatch, etc. You need to use mod_rewrite and match against the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^t=123$
RewriteRule ^forum/viewtopic\.php$ /page1.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=345$
RewriteRule ^forum/viewtopic\.php$ /page7.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=89$
RewriteRule ^forum/viewtopic\.php$ /page3.html? [L,R=301]
NOte that RewriteEngine is a mod_rewrite directive, not mod_alias. So it has no affect at all on the RedirectPermanent directives.

Apache htaccess rewrite (Pretty URLs)

I have a sneaking suspicion this is not possible, but figured I would ask regardless.
Is it at all possible to take a URL passed to a server in the form of:
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
And rewrite it to appear as:
http://domain.com/Controller/Action/1/2/3
I am trying to clean up an borderline ancient project to support "Pretty URLs" and I would really like to make the URLs display a bit nicer. I know I could setup a 301 header redirect to the new URL, but I would prefer to avoid that overhead if at all possible.
Any thoughts?
Thanks!
To get
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
To appear as
http://domain.com/Controller/Action/1/2/3
You will need to use %{QUERY_STRING} to capture the query string data. Your .htaccess file will look like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Action=Controller/Action&one=(\d+)&two=(\d+)&three=(\d+)
RewriteRule ^.+ /Controller/Action/%1/%2/%3 [R=301,L]
This will set up a permanent redirect to the new page. You can play around and test .htaccess rewrite rules here: htaccess.madewithlove.be
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?Action=$1/$2&one=$3&two=$4&three=$5 [L,QSA]

.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 mod_rewrite correctly on Debian server

the good old mod_rewrite. I can't seem to get it right.
Typical scenario: A user types in "http://domain.com/page"
I want that the user is being redirected to "http://domain.com/page/page2"
My htaccess file looks as follows:
RewriteEngine on
RewriteBase /var/www/
RewriteRule ^/page/$ page/page2
RewriteRule ^/bla/$ page/page2/bla
The first rewrite rule works, the second on the other hand doesn't seem to have any effect. Any idea? Maybe a better way to do this?
And another question:
As I said the first rewrite works just fine, but the url is not pretty. "http://domain.com/page" changes to "http://domain.com/page/page2". Is there a way to keep the typed in url but still forward the user to the actual link?
I presume the .htaccess is in your DocumentRoot.
How does your /bla containing look like? This should not rewrite the URL in the browser.
Use this:
RewriteEngine on
RewriteBase /
RewriteRule ^(/?)page/?$ $1page/page2 [L]
RewriteRule ^(/?)bla/?$ $1page/page2/bla [L]

Rewrite rule on multiple variables using Apache mod_rewrite

I would like to know how can I rewrite www.site.com?lang=lv&type=1&mode=2 into www.site.com/lv/1/2 using Apache mod_rewrite options.
Assuming that you actually want to rewrite /lv/1/2 to ?lang=lv&type=1&mode=2 (I see no reason to do the opposite) and that no other rewrites are active, this should do the trick:
RewriteRule ^/([^/]*)/([^/]*)/([^/]*)$ ?lang=$1&type=$2&mode=$3 [L]
Also; you'd be better off replacing those magic numbers with more useful information if you want to include them in your URI.
Edit: If it really is the opposite you'd like to do, see the answer by Matt S.
Basic rewrite:
RewriteEngine On
RewriteRule http://www.site.com/?lang=(.+?)&type=(\d+?)&mode=(\d+?) http://www.site.com/$1/$2/$3 [L,R=permanent]
Edit: Changed rewrite flags to force a permanent redirect
You need to handle the URI path and query separately. If the parameters appear in that very same order, you can do this:
RewriteCond %{QUERY_STRING} ^lang=([^&]+)&type=([^&]+)&mode=([^&]+)$
RewriteRule ^$ /%1/%2/%3? [L,R=301]
Otherwise you will need to put them in the right order before using this rule or take each parameter at a time.