1

Edit :

I want to redirect a url

to : www.example.com/location/sydney

from: www.example.com/rss.php?pickup=sydney

Problem is i cant get $_GET['pickup'] value from www.example.com/location/sydney and also location will also change for eg: it may be cairns,london etc. I have a file called rss.php in that i will get the value from $_GET['pickup']. I tried this :- Redirect /location http://deals.vroomvroomvroom.com.au/rss.php

but it gives me a url :- http://deals.vroomvroomvroom.com.au/rss.php/sydney when i type in the url www.example.com/location/sydney

Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79
  • Are you trying to rewrite or redirect? Do you want the user to see/use a URL like `http://www.example.com/location/sydney` but the code to handle that is index.php with an element for location in the GET? Or are you trying to have requests to `http://www.example.com/?location=sydney` cause the user to end up with `http://www.example.com/location/sydney` in their address bar? Or do you actually want both? – ghbarratt Jul 26 '12 at 13:20

6 Answers6

1

Try this in your .htaccess file

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Prathamesh mhatre
  • 1,015
  • 5
  • 17
  • 32
1

Try This one

RewriteEngine    On
RewriteCond      %{QUERY_STRING}    ^location=1$
RewriteRule      (.*)               $1?     [R=permanent]
Prathamesh mhatre
  • 1,015
  • 5
  • 17
  • 32
1

Place an .htaccess file in the location directory with the following:

RewriteEngine On
RewriteRule (.*) ../index.php?location=$1&e=1 [L,QSA]

This assumes that the index.php that you want to use for requests to /location is located in the directory above the location directory

If you also need the redirect (www.abc.com/?location=sydney to www.abc.com/location/sydney) to work then I think the best way to do that is to add the following to the (separate) .htaccess file located in the DocumentRoot:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)location=([^&]*)(.*) [NC]
RewriteCond %{QUERY_STRING} !&e=1 [NC]
RewriteRule ^(.*)$ $1/location/%2?%1%3 [R=301,L,NE]
ghbarratt
  • 11,496
  • 4
  • 41
  • 41
  • thanks for your reply, i have link which is-`www.abc.com/?location=sydney` i want to redirect this to `www.abc.com/location/sydney` also location can be anything.so I should I create `location` folder as well?srry if im wrong im new to this. – Rakesh Shetty Jul 27 '12 at 07:34
  • Okay, so if someone puts `www.abc.com/?location=sydney` in a browser address bar, you want the browser to redirect (changing the URL in the address bar) to `www.abc.com/location/sydney` BUT you also want to actually use the file DocumentRoot/index.php to answer that same request as if the request had been for `www.abc.com/?location=sydney` without any Apache rules, right? So basically you want a rewrite AND a redirect, right? – ghbarratt Jul 27 '12 at 13:43
  • yes @ghbarratt how to do that ? – Rakesh Shetty Jul 27 '12 at 13:59
  • Have a look at what I added to my answer. Let me know if works or not. – ghbarratt Jul 27 '12 at 14:09
  • ok @ghbarratt ill try this and let u know – Rakesh Shetty Jul 27 '12 at 14:23
  • When i write this :-`RewriteEngine On RewriteRule (.*) ../index.php?location=$1 [L,QSA]` its throwing an error->`400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.` and when i write this its redirecting properly but `The requested URL /location/Cairns was not found on this server.` its show this message – Rakesh Shetty Jul 27 '12 at 14:37
  • Okay, I changed both .htaccess contents in my answer. It is working in my own test environment, but could probably use a tiny tweak or two. – ghbarratt Jul 27 '12 at 15:18
0

Have you enabled mod_rewrite? sudo a2enmod rewrite

Also what's the output of tail -f /var/log/apache2/error.log when you hit refresh?

Eamorr
  • 9,872
  • 34
  • 125
  • 209
0

There are two possible problems you might have:

1) Is mod_rewrite enabled? Check with this command: apache2ctl -M | grep rewrite – you should see a line similar to rewrite_module (shared) if it's enabled. If not – enable it.

2) Do you have permissions to use .htaccess files? Check the configuration for your virtualhost – this should contain the line AllowOverride, similar to this:

<Directory "/var/www/your/site/">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

Now in my example mod_rewrite wouldn't work, because AllowOverride is set to None. Check what you need here: http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride but you basically need to set it to any other valid option, that is not None, otherwise Apache will completely ignore .htaccess files.

Edmunds
  • 56
  • 3
0

Location can be anything ...I tried follwoing code but its not working.

If you want to redirect from /location.php?location=sydney to /location/sydney, your RewriteRule is the wrong way around. Alas, you can't rewrite based on query parameters.

Berry Langerak
  • 18,561
  • 4
  • 45
  • 58