I have been trying to fathom this out for a little while and can't seem to come to a conclusion that always works.
I just need to replace a query string name see:
index.php?option=com_hikashop&ctrl=checkout&task=notify%C2%ACif_payment=bf_rbsbusinessgateway&componentStyle=common
we get the above which is the part I need to manipulate there are more query strings attached about 5-10 dependant.
I just need to change the section:
task=notify%C2%ACif_payment
to:
&task=notify¬if_payment
Can any mod rewrite experts provide a solution?
You can use this rule as your first rule in site root .htaccess:
RewriteCond %{THE_REQUEST} \?(.*&)?(task=notify)\%C2\%AC(if_payment)&?(\S*)\sHTTP [NC]
RewriteRule ^index\.php$ %{REQUEST_URI}?%1%2¬%3%4 [R=301,NE,L]
Make sure to clear your browser cache before testing this change.
Related
I've searched SO and Google long and hard for this one and I'm really surprised not to have found an answer (or stumbled upon the solution by trial and error!). It's a slightly tough search as most of the keywords lead to people wanting to exclude query strings as part of their redirect, whereas I want to exclude certain query strings from the subsequent redirect entirely.
We have migrated a content site from olddomain.com (running Drupal) to newdomain.com (running Wordpress). All the paths stay the same and so we want to redirect like-for-like from one domain to the other. However, we still want to be able to access Drupal's admin panel (and other associated admin URLs) for a variety of reasons. These exceptions must be done by exclusion so that, when we are not redirecting to the new domain, Drupal's existing generic mod_rewrite rules still activate in order to serve the redirect- excluded URLs correctly.
The main "like for like" redirect rule looked like this, and works well:
RewriteCond %{REQUEST_URI} !^/?(admin/|index.php|install.php|authorize.php|cron.php|update.php|xmlrpc.php|batch)
RewriteRule ^(.*) https://newdomain.com/$1 [R=301,L]
However, some admin-only paths (typically for editing a piece of content) don't always use "admin" in the path, e.g.:
/node/4823/edit
So what I want to do is to be able to manually add a noredirect query string variable which is then used as a further negated RewriteCond of my existing RewriteRule so in essence I am saying "do a like-for-like redirect on all paths as long as they are not in any of these folders and noredirect doesn't appear in the query string".
This is as far as I've got, you can see some of the steps I've taken, all of which have failed so far:
RewriteCond %{REQUEST_URI} !^/?(admin/|index.php|install.php|authorize.php|cron.php|update.php|xmlrpc.php|batch)
#RewriteCond %{QUERY_STRING} !^noredirect=([^&]+)
#RewriteCond %{QUERY_STRING} !^/noredirect/
#RewriteCond %{QUERY_STRING} !^.*(\bnoredirect\b)
#RewriteCond %{QUERY_STRING} !^.*(noredirect)
RewriteCond %{QUERY_STRING} !(noredirect)
RewriteRule ^(.*) https://newdomain.com/$1 [R=301,L]
As I've gone on I've tried to make it more and more generic in order to try and simplify the task; all I care about is checking for "noredirect" anywhere in the query string, so I'd be happy with all of these query strings matching the exclusion and thus preventing the redirect:
?noredirect
?noredirect=
?foo=bar&noredirect=whatever
?thisbitsaysnoredirectinit&foo=bar
As always, I look forward to basking in your expertise...
After much fiddling, and reordering and testing the rules separately (stupid I didn't do this before, this clearly showed the query string rule in itself was fine), this was the solution:
RewriteCond %{REQUEST_URI} !(^|/)(admin/|index.php|install.php|authorize.php|cron.php|update.php|xmlrpc.php|batch|node)
RewriteCond %{QUERY_STRING} !(noredirect)
RewriteRule ^(.*) https://newdomain.com/$1 [R=301,L]
So it required the change from ^/? to (^|/) in the "exclude these folders" rule.
I'll happily admit I don't understand why this has fixed it. Unfixed, this condition itself was working fine, it was just preventing the next condition from being looked at. If there was a problem with the syntax it seems to me that the condition should have been broken entirely rather than messing with other conditions. If anyone can shed any light on that for me, thank you!
For what it's worth, RewriteBase is unspecified (so I assume defaults to root).
Drupal-specific bonus
Also note the addition of "node" to the list of folders I'm excluding from the 1:1 redirect to the new domain.
When I thought about this more I realised that this would mean the admin-only extensions of "node" (e.g. /node/123/edit) would be safe as they are not rewritten to an alias. The public views of nodes (e.g. /node/123) would be initially ignored but then subsequently rewritten to their aliases by Drupal's own functionality, at which point .htaccess is called a second time and the redirect to the new domain (as the alias does not begin with "node") is activated.
This is not only a better system (anyone trying to go to an original Drupal node URL rather than an alias will not get redirected too soon) but of course means I now only have to use the ?noredirect query string in much rarer use cases.
How can modrewrite be done in this above situation:
Lets say the website us is: www.real-estate.com
Then we have the first mod rewrite:
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
So this will rewrite to something similar: www.real-estate.com/florida and it will list all the real estates in florida.
Then we add this rule:
RewriteRule ^([a-zA-Z\-]+)/(.*)+$ details.php?project=$2
This will rewrite to www.real-estates/florida/project-one and it will display details from that project.
But if I access the link for the city like this: www.real-estae.com/florida/ (with slash last) it will jump me to the second mod rewrite rule, taking me to details.php with an empty variable.
What is the correct solution to slove this problem, so that if the users adds a slash after the city it will still display the projects in that city and not the details page with empty var ?
After playing around I found that his works, but I do not know if it is the correct solution:
I replaced this:
RewriteRule ^([a-zA-Z\-]+)/+(.*)$ details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
With this:
RewriteRule ^([a-zA-Z\-]+)/([a-zA-Z\-]+) details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
So I have replaced (.*)$ Why does this work and the other way not I do not know.. Maybe someone can explain.
Probably it fixed the immediate problem but your rules might still match more than required due to faulty regex used and not using anchor $.
For your case use these rules:
RewriteEngine On
RewriteBase /
RewriteRule ^[a-zA-Z-]+/([a-zA-Z-]+)/?$ details.php?project=$1 [L,QSA]
RewriteRule ^([a-zA-Z-]+)/? cities.php?city_url=$1 [L,QSA]
Apache mod_rewrite Introduction
I'm having trouble understanding why this rewrite isn't doing what its told.
NOTE: the first rewrite in my .htaccess file works properly so its not a problem with using mod_rewrite on local host.
i have URIs which i know will be in the format:
http://localhost/managerhub/my-manager.php?i=1&t=dashboard
when site goes live:
http://themanagerhub.com/my-manager.php?i=1&t=dashboard
my .htaccess file reads thus:
RewriteEngine On
RewriteRule ^([a-z\-]+)$ $1.php [L]
RewriteRule ^my-manager-([0-9]+)-([a-z]+) my-manager.php?i=$1&t=$2 [PT]
To achieve clean URls like:
http://localhost/managerhub/my-manager-1-dashboard
Ideally i dont really want the first capture group ([0-9]+) since i dont really want the 'i' value in the resultant
clean url - so ideally id like:
http://localhost/managerhub/my-manager-dashboard
However ive not even got the rewrite to work so far at all having tried:
leading forward-slash on the target (though i dont think it was necessary)
tried changing the '&' ampersand in the target to use &
removing the [PT] passthru flag replaced with and without [L] flag
tried most 'least' restrictive character classes in the pattern i.e. (.*) instead of ([0-9]+)
commented 'out' the first RewriteRule which works flawlessly BTW - so using the troublesome rule in isolation
Non of these have worked - the second rewrite rule has no effect on the target urls so i cant even see were the discrepancy is. I'm still new to mod_rewrite so sort of rely on an informative fail so i can work out were my reg-ex is wrong but i suspect its just being ignored since im getting 'zilch' back!!
Any help appreciated - maybe with a pointer to my folly.
thanks
Your htaccess file, I'm assuming, is in the "managerhub" directory. That's where those rules need to be. You may need to add a base as well:
RewriteEngine On
RewriteBase /managerhub/
RewriteRule ^([a-z\-]+)$ $1.php [L]
RewriteRule ^my-manager-([0-9]+)-([a-z]+)$ my-manager.php?i=$1&t=$2 [L,PT]
which you'd need to change when they get to the live site. You can remove the first capture group via:
RewriteRule ^my-manager-([a-z]+)$ my-manager.php?t=$1 [L,PT]
The L flag isn't exactly 100% needed.
I have a rule that works for one "direction" but, not the other.
A typical incoming url / query would be: (long url)
http://somedomain.com/getme.pl?dothis=display&partnum=1234567 (could be up to 9 digits)
I have this rule in place in my htaccess file:
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L]
Which works great for a bit of ease getting one of the unique part numbers:
http://somedomain.com/1234567.
However, I would like to make the long url "pretty" so, I assumed I could reverse(ish) it.
So, when a link on the site is clicked on (the long url) the htaccess file would process the long url to the beautified version.
I tried MANY attempts.
Here was my latest failure.
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(works)
RewriteCond %{QUERY_STRING} ^partnum=([0-9]*) #(tried to get partnum)
RewriteRule ^.* http://%{HTTP_HOST}/%1 [R] #(make the short url)
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(the known working rule)
I have tried a plethora of rules and visited many sites for advice.
I tried with just rules, just conditions and variations of query_string.
So, I believe I must just grab the "partnum" from the query and rewrite to /1234567 or http_host/1234567
Then, allow the other rule (works) to process.
So BOTH:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and
http://somedomain.com/1234567
Display as: http://somedomain.com/1234567 in the browser.
and both passed the whole query to the getme.pl script properly.
I found some close answers here but, none that really explained what I needed.
Can someone please help?
From the sounds of it, this should get you moving down the right path:
# Your working rewrite, with extra param on the rewrite
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]
# Redirect for long urls, to pretty url
# -The appended '&rewrite' on the first rule will force this not to match
# -The trailing '?' on the rewrite will strip the query string
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]
Hope that helps.
I would like to redirect my users from an old domain to a new one using htaccess but also add a variable in the address to indicate they are coming from the older domain,
here's an exampel
http://old.com/index.php?var1=3 ==> http://new.com/index.php?var1=3&comingFromOld=1
http://old.com/index.php ==> http://new.com/index.php?comingFromOld=1
any help is greatly appreciated
Ok, so the normal basic redirect from old to new domains would be something like:
RewriteCond %{HTTP_HOST} ^old\.com$ [NC]
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]
To add in this extra URL variables, there are two cases we want to consider. Firstly, some pages will already have URL variables, in which case we just want to append &comingFromOld=1. Secondly some pages won't have any URL variables, in which case we want to append with ?comingFromOld=1 instead.
You'd expect this would maybe require either two sets of rules, or one complicated regex to allow for both cases. Fortunately there's a nice flag we can use, QSA, that will append the original query string (if one exists). I think this should cover you.
RewriteCond %{HTTP_HOST} ^old\.com$ [NC]
RewriteRule ^(.*)$ http://new.com/$1?comingFromOld=1 [QSA,R=301,L]
So you'll either end up with URLs like http://new.com/index.php?comingFromOld=1 or http://new.com/index.php?comingFromOld=1&var1=3