.htaccess redirect to change parameter value - apache

I would like to change the URL
/index.php?main_page=product_info&cPath=1&products_id=4
to
/index.php?main_page=product_info&cPath=141&products_id=302
I have tried
RewriteRule ^products_id=4$ products_id=302
but that doesn't seem to match.
My .htaccess file currently contains
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^products_id=302$ products_id=4 [L,R=301]
It would seem like I could use something like
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)products_id=4(&.*)?$
RewriteRule ^index\.php$ /index.php?%1products_id=302%3 [L,R=301]
but that doesn't work reliably. (If I click on a link, it doesn't work, but if I enter that URL in the address bar, it does.)

Try the following rule :
RewriteEngine on
RewriteCond %{THE_REQUEST} /index\.php\?main_page=([^&]+)&cPath=1&products_id=4\sHTTP [NC]
RewriteRule ^ /index.php?main_page=%1&cPath=141&products_id=302 [L,R]

Related

How to redirect url with question mark in .htaccess?

I need to change this URL:
site/nossos-socios.php?termo=test
to
site/nossos-socios/?q=test
I've already tried to use \? before q but doesnt work, this is my rewriterule in .htaccess:
RewriteRule ^nossos-socios/q\=([^/.]+)?$ nossos-socios.php?termo=$1 [NC,L]
You need to use RewriteCond to match a query string:
Options -MultiViews
RewriteEngine On
RewriteCond %{QUERY_STRING} ^q=([^&]+) [NC]
RewriteRule ^(nossos-socios)/?$ $1.php?termo=%1 [NC,L]

Remove string from url using htaccess

in my recent project, I need to remove some repeated characters from my url. More specific, I want to make this url
http://example.com/cgi-bin/file.cgi?ga_option1=a&ga_option2=b&ga_option3=c
become this, removed ga_ part
http://example.com/cgi-bin/file.cgi?option1=a&option2=b&option3=c
I tried this:
RewriteEngine On
RewriteBase /cgi-bin/
RewriteRule ^(.*)ga_(.*)$ http://%{SERVER_NAME}/$1-$2 [NC,R=301,L]
and this:
RewriteEngine On
RewriteBase /cgi-bin/
RewriteCond %{QUERY_STRING} "ga_" [NC]
RewriteRule (.*) http://example.com/cgi-bin/file.cgi?$1 [R=301,L]
but nothing works. Please give me some advice. Thanks.
This should work :
RewriteEngine on
RewriteCond %{THE_REQUEST} /cgi-bin/file\.cgi\?ga_option1=a&ga_option2=b&ga_option3=c [NC]
RewriteRule ^ /cgi-bin/file.cgi?option1=a&option2=b&option3=c [L,R]
If the query string is dynamic, then try this instead
RewriteEngine on
RewriteCond %{THE_REQUEST} /cgi-bin/file\.cgi\?ga_option1=([^&]+)&ga_option2=([^&]+)&ga_option3=([^\s&]+) [NC]
RewriteRule ^ %{REQUEST_URI}?option1=%1&option2=%2&option3=%3 [L,R]
The reason why your #2 code isn't redirecting the request is because you are appending the uri to uri instead of the query string.

changing url with get paramater in .htaccess not working

I am trying to change my url using htaccess but it doesn't seem to be working. I want http://example.com/blog_view?id=1 to be changed to http://example.com/blog-id/1/ but it isn't working. I have tried using this.
RewriteEngine On
RewriteRule ^blog-id/([^/]*)/$ /blog_view?id=$1 [L]
You might have to add a RewriteBase directive:
RewriteEngine On
RewriteBase /
RewriteRule ^blog-id/([^/]*)/$ /blog_view?id=$1 [L]
You can test your rules with this tool
You need one redirect and one rewrite rule (already existing):
RewriteEngine On
RewriteCond %{THE_REQUEST} /blog_view\?id=([^\s&]+) [NC]
RewriteRule ^ /blog-id/%1? [R=302,L,NE]
RewriteRule ^blog-id/([^/]+)/?$ blog_view?id=$1 [L,QSA,NC]

HTACCESS: Redirect VS RedirectMatch to redirect one page to another on same domain?

I have my HTACCESS file here (below), and I want to redirect /payments to /design/payments just to make it a little easier when sending the link to clients.
Here is the whole file:
<Files .htaccess>
order allow,deny
deny from all
</Files>
ErrorDocument 404 http://mrobertsdesign.ca/404
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^mrobertsonline\.ca [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.mrobertsonline\.ca [NC]
RewriteCond %{HTTP_HOST} ^www\.mrobertsdesign\.ca [NC]
RewriteRule ^(.*)$ http://mrobertsdesign.ca/$1 [L,R=301,NC]
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [NC]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?category=$1&page=$2 [NC]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?category=$1&subcat=$2&page=$3 [L,NC]
The file works fine, if you see anything that should be changed, please let me know as well. But it works fine, any ways...
To redirect the page "/payments" to "/design/payments", I understand I should use "redirect" as follows:
Redirect 301 /payments /design/payments
But when I add that, it redirects me to:
http://mrobertsdesign.ca/design/payments?page=payments
Now I don't understand what exactly I need to change, that's all really, all help would be appreciated!
You need to add a blocking condition before your rule
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [NC]
She gets fired with a request to /payments, adds the parameter and then the Redirect keeps those added parameters.
RewriteCond %{REQUEST_URI} !^/payments
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [NC,QSA]
As a side note may be you also want to add the [QSA][1] flag in the last 3 rules, because when you add parameters in a RewriteRule, the previous query string gets lost.

Should be a simple 301 redirect

What I think should be simple is just not working.
I have switched domains
Old URL example:
digital.photorecommendations.com/recs/2015/01/big-zoom-field-review/
New URL example:
photorec.tv/2015/01/big-zoom-field-review/
Really just switching domain and dropping the recs folder from the URL
Using http://htaccess.madewithlove.be/ to test and the outputs the correct URL
Options +FollowSymlinks
RewriteEngine on
RewriteBase /recs
RewriteCond %{HTTP_HOST} !^www\.digital.photorecommendations\.com$ [NC]
RewriteRule ^recs(.*) http://photorec.tv/$1 [L,R=301]
When I place this in the htaccess file I get 404 errors on all the pages except the home page. The htaccess file is inside the /recs folder. I have also tried it in the root directory of digital.photorecommendations.com and I get no results at all.
Any suggestions?
Thanks!
You have wrongly used negation in RewriteCond and regex also needs a fix. Use this rule in /recs/.htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?digital\.photorecommendations\.com$ [NC]
RewriteRule ^(index\.php)?$ http://photorec.tv/ [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?digital\.photorecommendations\.com$ [NC]
RewriteRule ^(.+)$ http://photorec.tv/recs/$1 [L,R=301,NC,NE]