Problems with mod_rewrite and 301 redirects from one dynamic url to another - apache

Hey all, I'm having all kinds of problems with a bunch of apache redirects just now and could really use some help!
I'm wating to put in a 301 redirect for a load of urls from a client's old site to their new site in the following format;
Old - page.php?pageNum_rs_all=0&totalRows_rs_all=112
New - page/sub?foo=bar
The values in the query sting for the old site don't in any way tie up to any ids or references on the new site, I only want to match that specific request and redirect to the new page.
It feels like I've tried just about every combination of rewriterule I can find online but still nothing seems to be working. This is running on Apache 2.2.
The rule I started with (and keep going back to) is;
RewriteRule ^/page.php\?pageNum_rs_all=0&totalRows_rs_all=112 /page/sub?foo=bar [R=301,L,NE]
Any help would be greatly appreciated!
c.

The reason that this doesn't work is that RewriteRule can't see the QueryString. To get at the Query String, you need to use RewriteCond. See http://wiki.apache.org/httpd/RewriteQueryString for examples of how this works.

I've always tended to use use a series of 301 redirects in the following manner
Redirect 301 /oldpage /newpage

Related

.htaccess RewriteRule not redirecting to the correct place

We found hundreds of 404 in one of our blogs, the previous team, deleted a lot of tags and articles. We are trying to create a 301 that redirects anything with /blog/forums in on WP app to WP /knowledge a different WP.
We have many instances like
blog/forums/topic/native-property-window/
blog/forums/user/hemo2/
blog/forums/topic-tag/wql/
We need the redirect to work no matter whats after blog/forums
and redirect them to
/knowledge
Any suggestions?
To redirect anything starting with blog/forums/ to /knowledge is pretty simple
RewriteRule ^blog/forums/ /knowledge [R,L]

redirect old url to new one in open cart

I looking for some help to solve this issue. i'm using open cart and made a product long time ago, of course i'm used SEO url for this product.
Ex. example.com/myproduct
but now i decided to change this url:
Ex. example.com/newproduct
it's now working and up in new url but google indexed that old url and i trying to redirect that old url to new one but no success.
first i tried mod_rewrite:
RewriteRule ^myproduct$ https://example.com/newproduct [R=301,L]
it's not working, then write with old method, 301 redirect:
Redirect 301 /myproduct https://example.com/newproduct
it's look like redirected but there is a problem, when i run that old url it redirect to new url like this:
example.com/newproduct?route=myproduct
it show 404 page not found. but if i using fake url for example:
Redirect 301 /oldtest https://example.com/newtest
it's working fine! i'm wondering why it's not working!
I suggest this is because OpenCart is handling it and interfering with what you are doing. Use your first idea:
RewriteRule ^myproduct$ https://example.com/newproduct [R=301,L]
But make sure it is at the top of your .htaccess file before anything else from OpenCart. Make sure it has RewriteEngine On before it.

htaccess url redirects with parameters not working

I did a search of previous questions about URL redirects with parameters, but none seem to speak to my particular problem. I'm not a programmer so I don't really know how to adapt other suggestions to my situation. Specific HTACCESS strings to try (and adapt for other URLs) would really help me.
I did an SEO restructure of my WP blog permalinks and I am finding that although the naked URLs are redirecting OK, URLs with parameters are not redirecting, they are going to a 404 error. I need URL parameters because my site is multilingual (Transposh plugin) so the "lang" parameter tells the site what language to translate the content to.
I think I may need to create a bunch of HTACCESS redirects that will redirect old URLs with a language parameter to the new permalinks for those URLs and pass the lang parameter through.
An example of this would be:
Source URL: /this-old-postname/?lang=(*)
Destination URL /blog/this-new-postname/?lang=$
There's no way to predict the URL pattern (although the parameter pattern is predictable) as each URL was tweaked for best SEO contribution.
I expect I'll need to write lots of these, each unique, so if you are able to provide an example can you please provide it for two redirects which would work for the following actual examples?
Source: http://www.travelnasia.com/thailand/bangkok/don-mueang-airport/?lang=zh
Destination: http://www.travelnasia.com/thailand/don-mueang-airport-bangkok/?lang=zh
Source: http://www.travelnasia.com/blog/map-attractions-bangkok-skytrain/?lang=zh
Destination: http://www.travelnasia.com/blog/bangkok-skytrain-bts-mrt-lines/?lang=zh
MOD_REWRITE is already enabled and standard redirects created in HTACCESS do work. I am pretty sure to achieve this I will need to use:
RewriteCond %{QUERY_STRING} ^lang=(.*)$
I understand this ensures the query string is read from the source URL.
I thought a redirect rule like this should work, but it doesn't:
RewriteRule ^test-redirect/$ http://test.travelnasia.com/destination/hanoi/hanoi-vietnam-destination-guide/$1 [L,QSA]
I've tried lots of other combinations but none of them seem to work.
Thanks in advance for any help offered.
Tony
If you don't mind including other query string parameters, [QSA] (query string append) is by far your simplest option.
Make it a 301 redirect with [R=301] if this is permanent change.
This should work for your purposes
RewriteEngine on # if not already enabled
RewriteRule ^thailand/bangkok/don-mueang-airport/?$ /thailand/don-mueang-airport-bangkok/ [QSA,R=301]
RewriteRule ^/blog/map-attractions-bangkok-skytrain/?$ /blog/bangkok-skytrain-bts-mrt-lines/ [QSA,R=301]
# ...

How to setup wild card 301 redirect that will remove URL parameters from index.php

ok I previously had some issues setting up a wild card redirect to strip parameters from an old url format that were non-seo friendly to our root. With the help of stackoverflow we've got it corrected and working but now I am experiencing issues with index.php?
Im seeing tons of duplicate urls in the engines using this url format index.php?cPath=# and Google is stressing me because they think its duplicate index pages.
I tried setting up this redirect in our htaccess but no go. It won't strip out the parameters or redirect it just duplicates the page with a valid 200 response.
RewriteRule ^index.php/.*$ /? [R=301,NE,NC,L]
I want to redirect anything with index.php?cPath=# to our root domain. Any ideas on how i can tackle this 301 redirect using a mod_rewrite on apache?
I've also just noticed that our site has another issue with this url format:
domain.com/?cPath=#...
so now I also need to write a rewrite for domain.com/?cPath=# I don't know where this one popped up from but I can see its going to cause issues hence the valid 200 response code it gives. The engines are really going to love me for this one.

Prevent users from accessing files using non apache-rewritten urls

May be a noob question but I'm just starting playing around with apache and have not found a precise answer yet.
I am setting up a web app using url-rewriting massively, to show nice urls like [mywebsite.com/product/x] instead of [mywebsite.com/app/controllers/product.php?id=x].
However, I can still access the required page by typing the url [mywebsite.com/app/controllers/product.php?id=x]. I'd like to make it not possible, ie. redirect people to an error page if they do so, and allow them to access this page with the "rewritten" syntax only.
What would be the easiest way to do that? And do you think it is a necessary measure to secure an app?
In your PHP file, examine the $_SERVER['REQUEST_URI'] and ensure it is being accessed the way you want it to be.
There is no reason why this should be a security issue.
RewriteCond %{REDIRECT_URL} ! ^/app/controllers/product.php$
RewriteRule ^app/controllers/product.php$ /product/x [R,L]
RewriteRule ^product/(.*)$ /app/controllers/product.php?id=$1 [L]
The first rule will redirect any request to /app/controllers/product.php with no REDIRECT_URL variable set to the clean url. The Rewrite (last rule) will set this variable when calling the real page and won't be redirected.