Remove string from url using htaccess - apache

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.

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]

htaccess: rewrite and redirect 301

I rewrited, by .htaccess, a category of dynamic url generated by a query string in this mode:
RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
Now my rewrite works in right way and, for example, the following urls drive to the same page:
http://www.mysite.it/id1-01234-id2-56789
http://www.mysite.it/page.php?id1=01234&id2=56789
But now I want a redirect 301, from second type to first type, for all dynamic urls. For example:
from
http://www.mysite.it/page.php?id1=01234&id2=56789
to
http://www.mysite.it/id1-01234-id2-56789
The following way doesn't work:
RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
RewriteCond %{QUERY_STRING} (^|&)id1=$1($|&)
RewriteCond %{QUERY_STRING} (^|&)id2=$2($|&)
RewriteRule ^page\.php$ /id1-id2? [L,R=301]
Where is the error?
can you help me please?
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/page.php
RewriteCond %{THE_REQUEST} \?id1=(\w+)&id2=(\w+)\s
RewriteRule ^page.php /id1-%1-id2-%2? [NC,R=301,L]
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id2=$2 [L]

.htaccess redirect to change parameter value

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]

Apache redirect using RewriteEngine with and without query string

I'm trying to get 2 redirections set on my htaccess file but I can't make it to work.
The desired output is:
if the user goes to www.mywebsite.com/page1, it gets redirected to /folder/newPage
if the user goes to www.mywebsite.com/page1?a=123, it gets redirected to /folder/anotherPage?a=123
I tried this
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/page1$ /folder/newPage [R=301,L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/page1(.*)$ /folder/anotherPage$1 [R=301,L]
I also tried something like this:
RewriteCond %{REQUEST_URI} ^/page1$
RewriteRule ^(.*)$ /folder/newPage [R=301,L]
RewriteCond %{REQUEST_URI} ^/page1$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ /folder/anotherPage%1 [R=301,L]
Note that Rewrite mod is turned on on my apache.
Any idea how to make it work?
Check through the following:
RewriteEngine On is present before all the rules
AllowOverride directive allows your htaccess files to load
htaccess receives URLs without the leading slashes, unlike server config files, or VHosts file.
.* matches 0 or more characters, thus %{QUERY_STRING} ^(.*)$ also matches empty query strings. Change this to .+.
Final rules should be:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^page1$ /folder/newPage [R=301,L]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^page1$ /folder/anotherPage?%1 [R=301,L]

Rewrite url using .htaccess file

I want to add https in a specified url
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^magsonwink.winkplatform.com/Shopping/paynow
RewriteRule ^(.*)$ https://magsonwink.winkplatform.com/Shopping/paynow%{REQUEST_URI} [QSA,R=301,L]
but this not worked for me. After googling i can't get a specified answer. if any one know about this please help me
Thanks in advance.
Here is the corrected code:
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^magsonwink\.winkplatform\.com$ [NC]
RewriteRule ^Shopping/paynow(?:/.*|)$ http%1://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
Above code will work both with HTTP and HTTPS
No need to use QSA flag since you aren't modifying query string
The %{HTTP_HOST} variable is only the hostname, no URL-path info is in it. So you need to remove it and add it to the pattern in your rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^magsonwink.winkplatform.com$ [NC]
RewriteRule ^/?Shopping/paynow(.*)$ https://magsonwink.winkplatform.com/Shopping/paynow$1 [QSA,R=301,L]