Redirection in htaccess for the same domain - .htaccess

I need to redirect from one url to another in the same domain, domain is www.example.com
From http://www.example.com/ca/ceca/callback to http://www.example.com/es/ceca/callback
I tried this but it doesn't work.
# Redirect url to other url in the same domain
Redirect /ca/ceca/callback http://www.example.com/es/ceca/callback
Maybe it worths to mention that the url is not really loaded in a browser but it is requested by a payment system to get a response from our website.

Try the following rule
RewriteRule ^(.*)ca/(.*)$ $1/es/$2 [R]
What
^(.*)ca/(.*)$
Does is that it searches entire request url for "ca/" pattern, and since you want to preserve stuff after ca, we enclose it in braces. Braces make sure it gets stores in variables.
Next we rewrite the url as $1, everything matched by first expression in first set of braces, that is anything before ca, then we write "es" and then append stuff from second set of braces, stores in $2. The [R] flag is to send the redirect response header to client with the new url.
Hope this helps.

Related

301 Redirect Issues in .htaccess file

I've been digging through the archives and threads and can't seem to find anything that matches my 301 Redirect issue -
I am trying to redirect old links to a new site and a problem with the following:
This one works - Redirect 301 /food-service/ http://www.xxxx.com/food-services.html
This one does not - Redirect 301 /food-service/distribution http://www.xxxx.com/distribution.html
The one that does not work tries to redirect to - http://www.xxxx.com/food-services.htmldistribution/
Would you mind lending me your thoughts on what I can do?
Thank you all!
The documentation for Redirect says:
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
The behaviour that you do observe is as we would expect from the documentation. It goes through the Redirect directives, and chooses the first one that matches.
To get the correct behaviour, you have to list the most specific redirect first, and the least specific last.
If you would have rules for /a/b/c, /a/b and /a, then you list them in that order.

301 redirect URL with query string, removing part of the beginning URL, keeping the full query at the end

I'm trying to find a redirect that will remove part of a URL (in the middle), but leaves the query string in place at the end.
I can do this fine via a single url redirect, but there are hundreds of these urls so I'm trying to find a rule that might be able to do for all of them in one fell swoop, so i don't have to make one for each and any new ones will get redirected automatically.
I'm trying to remove 'search.php' from the urls, here is an example:
www.site.com/products/search.php?rPage=/items/listing/detail_handler.jsp?code=219592&units=Feet&item_id=2624472
to redirect to:
www.site.com/products/?rPage=/items/listing/detail_handler.jsp?code=219592&units=Feet&item_id=2624472
Thanks for your time.
According to the documentation, the query string is passed through unchanged by default
RewriteRule products/search.php http://www.site.com/products/ [L,R=301]

htaccess redirect url with space in it

Stupidly, I have sent out a newsletter without checking the links. One of which is broken so I want to handle this with htaccess.
My URL that is being linked to is:
http://www.domain.com.au/www.domain.com.au/campers-and-motorhomes/ne%20w-zealand/camper-rentals/
where the actual page is:
http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/
Note the space in new zealand as well as the additional www.domain.com.au
How can I set this up in htaccess?
Thanks
Since you don't have to manipulate the URL, you can use a simple Redirect:
Redirect /www.domain.com.au/campers-and-motorhomes/ne%20w-zealand/camper-rentals/ http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/
Edit If Apache doesn't like the space unquoted as %20, try quoting the whole thing with a real space in there:
Redirect "/www.domain.com.au/campers-and-motorhomes/ne w-zealand/camper-rentals/" http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/
Edit2 If it's appending a query string, you will need to use mod_rewrite to get rid of the querystring rather than a simple redirect, I'm afraid.
RewriteEngine On
# If the request starts with www.domain.com.au, it is the broken link
# Rewrite to the proper URL and put ? on the end to remove the query string
RewriteRule ^www\.domain\.com\.au http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/? [L,R=301]

301 Redirect Adding Query to End of Target URL

I can't seem to find a solution that works for my particular situation, despite many others having similar issues. When I try to create a 301 redirect for a URL that has already been rewritten, the redirect works, but appends a query string to the end of the target URL, which references the URL to be redirected. For example:
Redirect 301 /dir1/dir2/dir3/ http://www.example.com/dir1/dir2/dir5/
results in
http://www.example.com/dir1/dir2/dir5/?&a=/dir1/dir2/dir3/
I don't believe the Redirect rule above is appending the QS params, so it is likely another rule in your .htaccess.
You also need to verify when the additional QS params are being added, before the first redirect or in a subsequent redirect. You can to this using an HTTP debugging proxy e.g. Fiddler
Alternatively, you can use the place the equivalent rule below at the top of your .htaccess, before any other rules and see if the extraneous QS params are still there.
RewriteRule ^dir1/dir2/dir3/$ http://www.example.com/dir1/dir2/dir5/ [NC,R=301,L]
If they are still present, something else in your .htaccess is matching http://www.example.com/dir1/dir2/dir5/ and adding the QS value
Posting the relevant portions of your .htaccess, or the entire thing if you can would help

How can I make my urls friendly from this example?

My site currently displays like this (when clicking on an ad):
hxxp://mysite.com/viewItem.php?id=10
I would LOVE for the urls to read:
hxxp://mysite.com/dogs/121/border-collie-for-sale-to-good-home
where "dogs" is forever a constant, 121 is always the $row['postId']; and "border-collie-for-sale-to-good-home" is always my $row['title'];
I know it probably won't be so easy as a quick answer here but would appreciate it if you could get me going in the right direction. I'm assuming a change to .htaccess is in order and I am just terrible at trying to decipher that.
Something that you can do is add a redirect at the top of the viewItem.php script. This redirect will need to check for a query parameter that you use to indicate that the .htaccess file as rewritten. Something like this:
if( !isset($HTTP_GET_VARS['rewritten']) ) {
// use whatever behind the scenes stuff you need to construct the friendly URL
$friendly_url = "http://mysite/" . getCategory() . "/" . getID() . "/" . getTitle();
// Now redirect the browser:
header("HTTP/1.1 301 Moved Permanently");
header("Location: $friendly_url");
exit();
}
// business as usual, the rest of your viewItem.php script.
So when this php script tries to handle a request that DOESN'T have the rewritten parameter in the query string, it redirects to the friendly version of the URL. If it DOES have rewritten it does what it usually does and handles the request.
Now in the .htaccess file, you want to rewrite the ugly URL to the friendly URL, but you need to include rewritten in the rewrite (I assume the "121" in your example is the "id" that the viewItem.php script takes:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/([^/]+)/ /viewItem.php?id=$1&rewritten=true [L]
If you leave out the &rewritten=true there, it's going to go into a redirect loop. You can also add a QSA in the brackets of the rule if you want to pass other query strings along with the rewrite in case you want to be able to handle friendly URLs like: http://mysite/dogs/121/border-collie-for-sale/?sort=asc With [L,QSA], the "sort=asc" gets passed along to viewItem/php.
So this is what happens now when someone clicks on an ad and gets taken to http://mysite/viewItem.php?id=121:
The browser location bar says http://mysite/viewItem.php?id=121 request is sent to mysite and viewItem.php is accessed
The top of viewItem.php sees that there is no rewritten param, so it redirects the browser to http://mysite/dogs/121/border-collie-for-sale
Since that was a 301 redirect, the browser's location bar changes to http://mysite/dogs/121/border-collie-for-sale
Request is resent to mysite but this time, the .htaccess rewrites /dogs/121/border-collie-for-sale to /viewItem.php?id=121&rewritten=true INTERNALLY, so the browser's location bar does not change.
The request makes it back to viewItem.php, this time, the script sees that there is a rewritten parameter, and does it's usual business and the request is served
You can use whatever name for the parameter flag you want, as long as none of the scripts are using it.

Resources