Cannot get 301 to work - .htaccess

I am working on some redirections and I can't get them to work. I am trying to redirect this page http://www.sparkpe.org/blog/category/spark-blog/?attachment_id=1073 to the homepage.
I tired several solutions like,
RewriteCond %{QUERY_STRING} ^attachment_id=1073$
RewriteRule (.*)$ /summit/summit-presentations/? [R=301,L]
RewriteCond %{QUERY_STRING} ^attachment_id=1073$
RewriteRule ^blog/category/spark-blog/(.*)$ http://www.sparkpe.org/blog/? [R=301,L]
and also I tried removing $ and ? but none of these solutions seem to be working. What could the problem be?

Related

301 redirect from URL with GET-parameters to the homepage

I have an old website with Joomla 1.5. It has some strange links with GET-parameters, like this:
http://www.primavista.ru/images/stories/catalog/?rand=1186511674
http://www.primavista.ru/images/stories/catalog/?rand=145388433
http://www.primavista.ru/images/stories/catalog/?rand=1553907057
http://www.primavista.ru/images/stories/catalog/?rand=1563973527
http://www.primavista.ru/images/stories/catalog/?rand=1981273478
http://www.primavista.ru/images/stories/catalog/?rand=2139631800
http://www.primavista.ru/images/stories/catalog/?rand=366928750
http://www.primavista.ru/images/stories/catalog/?rand=524689684
http://www.primavista.ru/images/stories/catalog/?rand=569077423
http://www.primavista.ru/images/stories/catalog/?rand=573405687
http://www.primavista.ru/images/stories/catalog/?rand=879649167
I want make redirect theses links to the homepage.
I tried some different instructions in .htaccess:
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=([0-9]*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=(.*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} (^|&)(rand)=[^&]+ [NC]
RewriteRule ^images/stories/catalog(/.*)?$ https://primavista.ru/? [R=301,L,NC]
But no one not working. Maybe here someone can help me with this. Thanks
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ / [R=301,L]
It is a good idea to start out with R=302 temporary redirections and to only change that to R=301 permanent redirections one you are satisfied with everything. That prevents nasty caching issues on the client side ...
UPDATE:
Your comment below indicates that you actually ask to remove the GET parameter in the redirected request, which you never mentioned before...
You can use the additional QSD flag for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ /? [R=301,QSD,L]

redirect with htaccess and parameters

I moved from a wordpress to a typo3 site. On my old sites, the links looked like this:
http://www.example.org/?page_id=44
now I want to redirect it to:
http://www.example.org/contact
Usually the redirects-rules are no problem, but this time I don't get it why it is not working:
I tried this:
Redirect 301 /?page_id=44 http://www.example.org/contact
as well as this:
RewriteRule http://www.example.org/\?page_id=44 http://www.example.org/contact [R=301,L]
and this here:
RewriteCond %{REQUEST_URI} ^example.org/\?page_id=44
RewriteRule ^(.*)$ http://www.example.org/contact [R=301,L]
Tried it in several browsers and incognito-mode, but it still remains wrong, it still adds the parameter instead of redirecting to the certain page.
I guess it is somehow doable with %{query_STRING} ?
Is it due to the param-thingy?
Any ideas how to solve this problem?
Okay, got it. It had something to do with the query string:
Final result:
RewriteCond %{QUERY_STRING} ^page_id=14$ [NC]
RewriteRule ^(index\.php){0,1}$ /kontakt/? [L,R=301,NC]
You can use:
RewriteCond %{THE_REQUEST} /\?page_id=44\s [NC]
RewriteRule ^/?$ /contact? [R=301,L]

how to redirect from modules.php?name=News&file=article&sid=XXX to domain.eu/XXX?

I would like to redirect all url requests with the phpnuke string modules.php?name=News&file=article&sid=XXX (xxx is a number) to my domain-name.eu/XXX . How is that possible?
i tried
RewriteCond %{QUERY_STRING} ^(.*&|)modules.php?name=News&file=article&sid=(&.*|)$
RewriteRule ^(.*)$ %1domain.eu/%2 [L,R=301]
but it has no effect. Where is the mistake?
Also i don't understand why a simple
RewriteRule ^(.*)$ /modules.php?name=News&file=article&sid=$1 [L,R=301]
doenst work. Any ideas?
Thanks a lot for any help.
Not sure why I'm seeing this in Drupal questions. But try this.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/modules\.php$
RewriteCond %{QUERY_STRING} ^sid=([0-9]*)$
RewriteRule ^(.*)$ http://domain.eu/%1 [R=301,L]
You can't match everything after the sid=(.*) regex part in the URL in most cases. modules.php?name=News&file=article&sid=XXX and modules.php?sid=XXX&name=News&file=article are the same in functionality.
Learned at http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/

Redirect only certain requests to controller file using RewriteRule

I've created a controller file which handles redirecting users to the proper url, I want to direct certain traffic to this controller and not affect the rest. http://website.com/shows.php?id=review-1 is an example of the url I want to send to the controller.
What I've concocted thus far is the following RewriteRule which causes a site-wide server 500 Server Error.
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-[0-9]+(?:&|$) [NC]
RewriteRule ^ /controller.php [QSA,L]
Removing the -[0-9]+ from the %{QUERY_STRING} condition gets rid of the site-wide 500 Server Error, but doesn't work. The following RewriteRule that I'm trying to replace works just fine.
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=review-1(?:&|$) [NC]
RewriteRule ^ /review/1/super-baseball-2020/? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ /shows.php?id=$1-$2 [QSA,L,NC]
The end result is that a user who goes to http://www.website.com/shows.php?id=review-1 will end up at http://www.website.com/controller.php?id=review-1 which will deal with handling the 301 redirect.
I still don't know why you're trying to make simple things into complex ones ;)
6 minutes to find+test. Here's what should work:
RewriteCond %{QUERY_STRING} (^|(.)+&)(id=review-([0-9]+))($|&.*) [NC]
RewriteRule /shows.php /controller.php?%3 [QSA,L]
PS: Like Devin, I don't get/know why (?:^|&) works...
Interesting. Doesn't' look wrong, but I couldn't get the line
RewriteCond %{QUERY_STRING} (?:^|&)id=review-[0-9]+(?:&|$) [NC]
to compile in mod_rewrite until I took out the non-capturing indicators
RewriteCond %{QUERY_STRING} (^|&)id=review-[0-9]+(&|$) [NC]
seems to compile ok on my system, does that work for you?
I think you need to ascertain what's causing the 500 error. I suspect the redirect is working, but the page you're being redirected to is failing.

htaccess redirect not working as expected

I know I am missing something simple, but I can't get this redirect to work right. I have a parked domain that I want to redirect to the main domain. I am using:
RewriteCond %{HTTP_HOST} ^parked-domain.com$ [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
The problem is that parked-domain.com/anypage.html redirects to main-domain.com and leaves off the rest of the URL. I need parked-domain.com/anypage.html to redirect to main-domain.com/anypage.html
EDIT
I am sure this is a cache thing with my browser but after I tried the first suggestion then changed it back, now I get this:
parked-domain.com/anypage.html redirects to www.main-domain.com/anypage.html
but
www.parked-domain.com/anypage.html does not redirect at all.
Part of the problem was with the cache, that got part of the redirect working. I ended up using this to get it all forwarding properly:
RewriteCond %{HTTP_HOST} ^parked-domain.com$ [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.parked-domain.com$ [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
The first one should have worked for all cases, but it didn't. Adding the second redirect fixed it.
Try removing the ^ $ around your (.*)
RewriteRule (.*) http://www.main-domain.com/$1 [L,R=301]

Resources