301 Rewrite Rule Not working - apache

We want to redirect Below mentioned URL.
From URL:
www.example.com/itemLevelFilterPage.action?keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0
To URL : www.example.com/4G
We wrote 301 rules in apache configuration as below.
RewriteEngine on
RewriteRule ^/itemLevelFilterPage.action?keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0 /4G [L,R=301]
But redirection is not working as expected. Any Suggestion will be highly appreciated.

mod_rewrite will not look at query strings as part of its matching, unless you specifically ask it to:
RewriteCond %{REQUEST_URI} ^/itemLevelFilterPage\.action$
RewriteCond %{QUERY_STRING} ^keyWordTxt=&srchTyp=CATNAV&attrFilterList=attr_brand%3A%223M%22&resultPage=0$
RewriteRule (.*) http://www.example.com/4G? [R=301,L]
The first condition makes sure we're looking at the right page, the second checks the query string, and the rule will rewrite the URL to the desired one.
The trailing question mark on the rule ensures the old query string is removed. If you're on Apache 2.4 the query string discard flag is available:
RewriteRule (.*) http://www.example.com/4G [R=301,L,QSD]
More examples here.

Related

Rewrite in HTACCESS

Can anyone help me here? I did a rewrite for the english version of my site.
It's was mysite.com/?lang=en And now it's mysite.com/en/
I've tried redirecting the URL with this :
RewriteRule ?lang=en$ /en/ [R=301,L]
But each time I put this line in my HTACCESS, the website goes down.
Your rewrite rule is trying to use the parameter portion of the URL. There is an existing SO answer with an example:
HTAccess Rewrite based upon a Parameter Value, ignore other Parameters
Perhaps something like this will help:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)(lang=en+).+ [NC]
RewriteRule ^en$ %{REQUEST_URI}?%1 [R=302,NE,NC,L]

301 redirect ?___store=default

I need to make 301 redirection for all urls that have this kind of ending in them:
?___store=default&___from_store=com
For example:
301 FROM URL
https://www.example.com/page.html?___store=default&___from_store=com
TO:
https://www.example.com/page.html?___store=default_migrated&___from_store=com
Server has Apache in it and Magento 2 is the CMS I have running there. If more details are needed I'm happy to provide them.
You could setup a RewriteRule in apache for this. Basically you would just need to check the %{QUERY_STRING} server variable to see if it matches the query string that is exactly "?___store=default&___from_store=com" (you can change it to be a bit less strict in the matching with a regex if needed).
RewriteCond %{QUERY_STRING} ^___store=default&___from_store=com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1?___store=default_migrated&___from_store=com [L,R=301]
You can read more about Apache RewriteRules here.

Mod_rewrite (htaccess) QSA and removing charactes from appended query string

I've been struggling with some htaccess redirects. I just spent some time reading and searching and couldn't get a solution that works with my scenario.
I'm in the process of making the 301 redirect for an old website (ASP) to a new one (Wordpress). The old pages has parameters query which I need to process but also remove 'http://' string from it to get redirect to work.
Example URL (old) to redirect looks like:
http://www.domain.org/index.asp?documentID=2410&utm_source=IT+Travel+Reminder&utm_medium=Informz%2FnetFORUM&utm_campaign=IT%2FTravel+Reminder%2FMonthly+Monthly+Travel+Reminder&zbrandid=4050&zidType=CH&zid=28841368&zsubscriberId=1036792259&zbdom=http://my.informz.net
redirected to:
http://www.domain.org/permalink-2410/?qs=true&utm_source=IT+Travel+Reminder&utm_medium=Informz%2FnetFORUM&utm_campaign=IT%2FTravel+Reminder%2FMonthly+Monthly+Travel+Reminder&zbrandid=4050&zidType=CH&zid=28841368&zsubscriberId=1036792259&zbdom=my.informz.net
and .htaccess code to redirect it:
RewriteCond %{QUERY_STRING} ^documentid=2410(&.*)$ [NC]
RewriteRule ^index\.asp(.*):(.*)$ http://www.domain.org/permalink/?qs=true%1%2 [L,R=301,QSA]
but somehow is not working as I have expected when
RewriteCond %{QUERY_STRING} ^documentid=2410(&.*)$ [NC]
RewriteRule ^index\.asp$ http://www.domain.org/permalink/?qs=true%1 [L,R=301,QSA]
works fine when I will remove http:// or : from a query string.
Where do I have made mistake?
Thanks!
Try this rule:
RewriteCond %{QUERY_STRING} ^documentid=(\d+)(&.+?)http://(.+)$ [NC]
RewriteRule ^index\.asp$ http://www.domain.org/permalink-%1/?qs=true%2%3 [L,R=302,NC,NE]
Make sure to clear browser cache before testing this.

removing directory in apache mod_rewrite

I have a PHP site which replaces an ASP site, so the path structure is different.
In the URLs, I need to match http://apache.site/Cartv3/Details.asp & redirect to another location. What is the correct syntax to match that URL fragment?
I've already tried
RewriteCond %{REQUEST_URI} CartV3/results1.asp?Category=60
RewriteRule ^(.*)$ home-study/A-Levels/1/page-1 [R=301,L]
and
RewriteRule ^CartV3/Details\.asp?ProductID=1004 home-study/A-Levels/1/page-1 [R=301,L]
You meed to read more about mod_rewrite. Remember RewriteRule doesn't match query string. You attempt needs to be rewritten as:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^Category=60$ [NC]
RewriteRule ^CartV3/results1\.asp$ /home-study/A-Levels/1/page-1? [R=302,L,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
PS: ? after page-1 is a special mod_rewrite syntax to strip original query string. If you want to keep original query string in rewritten URL then take out ? in the end.
The problem here is that you are trying to match the query string, which has to be done by a separate RewriteCond. If you want the match specifically "Category=60", then you can add it as a Condition:
RewriteCond %{QUERY_STRING} Category=60
RewriteCond %{REQUEST_URI} /CartV3/results1.asp
RewriteRule .* home-study/A-Levels/1/page-1?
This will match http://example.com/CartV3/results1.asp?Category=60 and redirect. The ? at the end of the rule stops "?Category=60" being to the resulting URI.
If you don't care about the value in the query string, then you can remove the first condition.

mod_rewrite weird problem

I have a strange problem with mod_rewrite, the rules that are relevant here are:
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/igre\-(.*)\.php$ game.php?GameUrl=$2&Page=1 [L]
And a corresponding URL might look something like this:
example.com/miselne-igre/igre-shirk.php?Page=2
example.com/miselne-igre/igre-shirk.php
The problem is that the first rule has no effect. If I use the first URL from the example I always get 1 into the Page variable, which shows that the second rule is used.
So what's wrong with the first one? And why is the second rule even matching a URL with ".php?Page=XYZ" at the end, if I said that the URL ends with ".php"?
ps: other rules in the .htaccess file are working fine...
The query string is not part of the URI path that is being processed by the RewriteRule directive. You have to use the RewriteCond directive to process the query string.
RewriteCond %{QUERY_STRING} ^Page=[0-9]+$
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&%0 [L]
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&Page=1 [L]
But you can still simplify this by using the QSA flag (query string append):
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1 [L,QSA]
mod_rewrite is not using the query in it's rewriting process. Therefor you first RewriteRule is ignored. You could combine it with a RewriteCond (haven't tested it though) like so:
RewriteCond %{QUERY_STRING} Page=([0-9]+)
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2 [L, qsappend]
# qsappend appends the original query, in this case (Page=xx)
Ah, like Gumbo said; you can also use %1 to back reference to the page numer.
Is it just me or are your arguments back-to-front?
Do you mean:
RewriteRule ^(.*)\/(.*)\-igre\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/(.*)\-igre\.php$ game.php?GameUrl=$2&Page=1 [L]
You wanted to match miselne-igre not igre-miselne.
Obviously this doesn't address the main issue, but thought I'd throw that in.
Dom