.htaccess help, replacing URI segment - apache

I have a URL that looks like this...
http://www.domain/a/var
I am hoping it would be possible to rewrite the URL using .htaccess so that /a/ would be replaced by /b/.
Is that possible, and if so, how?

RewriteEngine On
RewriteRule ^a/(.*) /b/$1

This should do that you need
RewriteEngine on
RewriteRule ^/a/(.*) /b/$1
Note: You'll need to add [PT] or similar if you need mod_alias or mod_index or similar to work using the new path.

Related

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]

simple url to url via RewriteRule in htaccess not working

I need to redirect a single URL to another single URL, no parameters or anything
domain.com/snow to
superdomain.com/longer/url/snow
i am trying with, in .htaccess the following:
RewriteRule ^snow http://superdomain.com/longer/snow [R=301,L]
or
RewriteRule ^snow$ http://superdomain.com/longer/snow [R=301,L]
But none of them are working, any idea what am I missing?
Note: the url's are only examples but they have same structure than what i need
just found out! /snow/ flolder had its own .htacess :)
If you don't need parameters or anything, RedirectPermanent is a simpler option:
RedirectPermanent /snow http://superdomain.com/longer/snow

Rewriting url using mod-rewrite and apache

I'm trying to add some rewriting on my site, but it seems to not work, I'm using apache and .htaccess.
The code in my .htaccess file is:
RewriteEngine On
RewriteRule ^/?os_framework/?$ /os_framework/index.php?module=home [L,NC,QSA,PT]
This should send http://localhost/os_framework/ to http://localhost/os_framework/index.php?module=home
But it seems not to.
Any help would be appreciated.
In advance, thanks
Edit: Fixed the above, shouldn't have the os_framework/ in the search pattern, however now i cant get this one to work:
RewriteRule ^/(.[^/]*)/?$ /os_framework/index.php?module=$1 [L,NC,QSA,PT]
And what is wrong with
RewriteRule ^(.[^/]*)/?$ /os_framework/index.php?module=$1 [L,NC,QSA,PT]
Why does that throw a error 500? it should work
Try this:
RewriteEngine On
RewriteRule ^os_framework/?$ os_framework/index.php?module=home [L,NC,QSA]

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.

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]