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

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]

Related

Redirect URL with query string to different file and maintain query string

I have old URLs that use the following:
mydomain.com/?page=article&cid=149&aid=1554
I had to change the name of the old index.php (which that URL above 'used'), for various reasons... and the file name is now index_180311.php. So those old links no longer work. The query string that does not change is the 'page=' part... the rest can be variable.
So what mod rewrite command would I need to make old URLs with the 'page=' query string get redirected to the new file name/path at:
mydomain.com/index_180311.php?page=article&cid=149&aid=1554
so that they work again?
I've searched the boards and tried altering other htaccess commands from other threads, but have not been successful (it usually ends up crashing my whole site), as I think I created infinite loops.
Thanks much in advance.
I wouldn't do an external redirect, try something like
# check for the presence of the page parameter (and a value)
RewriteCond %{QUERY_STRING} ^(?:.*?&)??page=[^&]+
# silently pass requests with the parameter to the old one (the query string is passed on automatically when the substitution has no "?")
RewriteRule ^(?:index\.php)?$ index_180311.php [END]

HTAccess Redirect using main parameter, ignore all others

firstly I know and understand how to redirect based on parameters :)
My issue is that I need to redirect all links based on the supplied MenuID parameter and ignore any other information in the query string, as not all parameters are used in each web page request, e.g. menuid=2738421; is New Products
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products& limit=5&page=24
or,
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=20&page=3
or,
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&page=12&limit=15
to
http://www.domain.com/new.html?page=x&limit=x
The reason for the redirection is that search-engines have indexed these pages and so I need to prettify the URLs.
Is this actually possible to create a fuzzy redirect criteria?
## 301 Redirects
# 301 Redirect 1 - works for this explicit URL, but need a partial result
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^new\.html$ http://www.monarchycatering.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=5&page=24 [R=301,NE,NC,L]
Any help gratefully taken, thank you in advance
Mark.
Sorry for the delay, but StackOverflow doesn't seem to have a way to flag answers that have been replied to and need my attention.
OK, if I understand you correctly, you have an incoming "reduced" semi-SEF URL out in the real world (produced by your links), such as
http://www.domain.com/new.html&limit=5&page=24
("real" SEF would be something like http://www.domain.com/new/limit/5/page/24.html)
and you need to use .htaccess to map it to real files and more Query String information:
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=5&page=24
You want to detect new.html for example, and replace it by a fixed string shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&, using the [QSA] flag to append the existing Query String on to the end?
RewriteEngine On
RewriteRule ^new\.html /shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products [QSA]
RewriteRule ^sale\.html /shop.php?menuid=32424&menuref=Ja&menutitle=Products+On+Sale [QSA]
...etc...
I believe that a & will be stuck on the end of the rewritten address if the user supplied a non-empty Query String, but be sure to test it both ways.
P.S. It probably would have been cleaner to use "comment" to reply to my question, rather than adding another answer.
It's not clear to me what your starting point is and where you're trying to end up. Do you have "pretty" URLs that you want to convert into "non-pretty" Query Strings that your scripts can actually digest?
The reason for the redirection is that search-engines have indexed
these pages and so I need to prettify the URLs.
If the search engines have already indexed the Query String non-pretty version, they'll have to now re-index with pretty URLs. Ditto for all your customers' bookmarks.
If you want to generate "pretty" links within your site, and decode them (in .htaccess) upon re-entry to non-pretty Query Strings, that should work. Your customers' existing bookmarks should even continue to work, while the search engines will replace the non-pretty with the pretty URLs.
and thanks for the interest in my question...
I have rewritten parts of my website and Google still has references to the old MenuID parameter and shop.php configuration, but now I rewriten the Query to a prettier format, e.g.
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=5&page=24
is now
http://www.domain.com/new.html&limit=5&page=24
The pages represent product categories, and so needed to be displayed in a more meaningful manner. Customer bookmarking is not an issue, as long as I can redirect the pages.
I hope that makes sense, best wishes,
Mark.

Htaccess parameter overwrite + allow additional params?

I have a rule in my .htaccess file to overwrite ugly parameter urls with clean paths.
Here is an example:
RewriteRule ^landing(/)?([0-9]+)?(/)?$ landing/index.php?intPage=$2
So when someone goes to /landing/3, it would load the index.php page passing intPage parameters as 3.
Very simple.
However, I'm using techniques to trace Google Analytics referals, so I need to tack parameters onto this URL:
For example:
/landing/3?kt_type=ad&kt_st1=adwords&kt_st2=prize_a_us.en
The problem is that, I don't know how to retrieve the parameters that are tacked onto the URL, since the URL has already been overwritten and now I can't retrieve kt_type, kt_st1 etc.
Any idea on how to make it possible so I can still tack parameters to already overwritten URLs?
Use QSA flag. Change your rule to this:
RewriteRule ^landing/?([0-9]+)?/?$ landing/index.php?intPage=$1 [L,NC,QSA]
From official docs:
QSA - Appends any query string from the original request URL to any
query string created in the rewrite target

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]

Remove trailing ? [question mark] from url - empty query string

I am moving a website to a new Open Cart install and I have set up 301 redirects to keep any SEO value.
I had to use the following trick here - http://www.itsadam.co.uk/opencart-301-redirect-not-working-seo-fix-route/ - to work around an annoying issue with the way Open Cart handles url rewrites. So, I have this sort of thing in my .htaccess file currently:
redirect 301 /products-page /products?
However, this now leaves my rewritten urls with a trailing ? - an empty query string effectively:
http://www.mysite.com/products?
Is there a way I can use rewrites to match and ditch any superfluous question marks? (I need to retain any actual query strings).
I encountered the same issue in opencart. Rather than follow the link to your solution do it like this:
RewriteRule ^original-url$ new-url [L,R=301]

Resources