htaccess - how to redirect url with params to base url - apache

I am trying to redirect a page.html with params at the end to the base url without params. I know I am supposed to use mod rewrite but I can't get the rules figured out. Your help will be appreciated.
Here is the scenario:
I tried this in htaccess and this rule didn't work:
redirect 301 "/page.html?p=2" http://www.domain.com/page.html
I understand from reading other posts on stackoverflow that I should write something like
^page\.html$ http://www.domain.com/page.html [QSA,NC,R=301,L]
The rules at the end [QSA,NC,R=301,L] however are incorrect. I can't figure it out. I simply want to do a 301 for this url with the p=2 parameter, and no other param.
And then process the remaining rules after this rule for other pages.

You can put this code in your root htaccess
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=2$ [NC]
RewriteRule ^page\.html$ /page.html? [R=301,L]

Related

RedirectMatch adding extra url segments

I'm trying to redirect requests for our old blog to a new url on a subdomain.
The old url looks like
https://www.website.com/blog-name/post/slug-of-the-title
and it needs to redirect to
https://stage.website.com/blog-name/slug-of-the-title
I'm using this rule in my .htaccess
RedirectMatch ^/blog-name/post/(.*)$ https://stage.website.com/blog-name/$1
And I'm getting redirected to the correct page, but my urls have extra segments on the end. Like
https://stage.website.com/blog-name/slug-of-the-title/?/blog-name/post/slug-of-the-title
What am I doing wrong?
With your shown samples, could you please try following. Please clear your browser cache before testing your URLs. Make sure you keep these rules at the top of your .htaccess rules file(in case you have any more rules also in your .htaccess file).
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?website\.com$ [NC]
RewriteRule ^blog-name/post/(.*)/?$ https://stage.website.com/blog-name/$1? [NC,R=301,L]

redirect all URLs to domain root with htaccess rewrite

I am trying to get all requests including get variables to redirect to another domain with an htaccess rewrite,
I am trying to redirect all URLs from from my old blog to http://www.passiveincomeidea.com domain root but any URLs with a variable are still passing the variable to the new domain, like this one for example http://mika.lepisto.com/?p=2223
Here is what I have currently:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://www.passiveincomeidea.com/ [L,R=301]
How can I get the rewrite to strip all the variables during the redirect?
Thank you!
Append a question mark at the end of redirected URL:
RewriteRule ^(.*)$ http://www.passiveincomeidea.com/? [L,R=301]
For documentation, search for "Modifying the Query String" in the mod_rewrite documentation.

How to rewrite /?a=a to /a/?

How can I rewrite this URL:
http://mywebsite.com/?a=a
To this URL:
http://mywebsite.com/a/
With htaccess?
I tried this rewrite rule but it's not working:
RewriteRule /a /?a=a
Try this instead :
RewriteRule ^a/?$ /?a=a [L]
This will route http://mywebsite.com/a/ requests to http://mywebsite.com/?a=a
If you are only looking for an exact match of ?a=a in the URI you can use this.
RewriteRule ^\?a=a$ /a/ [R=301]
This assumes you want to rewrite the actual URL in the user's browser and provide a 301 header to let anything crawling the page know about the "proper" URI to use (thus the [R=301]).

htaccess URL Rewrite not working after 301 redirect

I have the following url rewrite:
RewriteRule ^info/([^/\.]+)/?$ info.php?page=info&subpage=$1 [L]
Originally the parameters were the page id's i.e. 0-10.
I have now changed this so the URLs have more meaningful slug names to reflect the content.
I have now set up the 301 redirects, for example:
Redirect 301 /info/0 http://www.example.com/info/intro
But the problem is, the redirect doesn't go to the url rewrite (http://www.example.com/info/intro). Instead it shows the full url (http://www.example.com/info.php?page=info&subpage=0)
How can it get it to keep the rewrite?
Many thanks
As posted by LazyOne, the solution was to use RewriteRule:
RewriteRule ^info/0$ http://www.example.com/info/intro [R=301,L]

Redirecting URLs (with specific GET parameters)

I have this old survey link that is has been superseded by another link, so basically I want anyone trying to access the URL:
http://mywebsite.com/survey/view_survey.php?surveyID=1
To be redirected to:
http://mywebsite.com/survey/view_survey.php?surveyID=2
Can I do this in the Apache configuration or htaccess file?
I tried the following rule in the Redirect section of my httpd.conf file:
Redirect 301 /survey/view_survey.php?surveyID=1 http://mywebsite.com/survey/view_survey.php?surveyID=2
But it doesn't work. I am suspecting that the GET parameters are not used when processing the rule.
Is my only option to hack my code to redirect on a specific surveyID?
Following the suggestion of using the Rewrite rules, I tried the following in my .htaccess file:
RewriteRule ^survey/view_survey\.php\?surveyID=1525$ /survey/view_survey.php?sur
veyID=1607
But that doesn't work. I do have the rewrite engine up and running, because I have another rewrite rule currently running.
Try this in a .htaccess file:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|.*&)surveyID=1525(&.*|$)
RewriteRule ^survey/view_survey\.php$ /survey/view_survey.php?%1surveyID=1607%2 [L,R=301]
RewriteEngine On
RewriteCond %{QUERY_STRING} ^surveyID=1525$
RewriteRule ^/survey/view_survey\.php /survey/view_survey.php?surveyID=1607 [R=301]
Check out the QSA portion of the mod_rewrite.
It does GET string manipulation.
There might be a possible duplicate of this question and it is solved if this solution doesnt work for you:
Apache Redirect 301 fails when using GET parameters, such as ?blah=