Writing a RewriteRule with just one exception - apache

For a project I need to write a RewriteRule in a .htaccess file but unfortunately I have nearly no experience how to write such rules. What I want to do is rather simple: A complete redirect from one path to another with just one exception. Let me show you my try:
RewriteCond %{REQUEST_URI} !^/success$
RewriteRule ^.*/wop_de/checkout/onepage.*$ http://%{HTTP_HOST}/wop_de/onestepcheckout/ [R=301,L]
What I have thought: If there is no string like "/success" in the URL do a redirect from "/wop_de/checkout/onepage/" to "/wop_de/onestepcheckout/". Well, I guess I was thinking the wrong way as it doesn't work. Could you help me, please?
Also do you know a good tutorial to learn how to write such rules? Thank you in advance!

Use this rule:
RewriteRule ^wop_de/checkout/onepage.*$ /wop_de/onestepcheckout/ [R=301,L]
.htaccess is per directory directive and Apache strips the current directory path (leading slash) from RewriteRule URI pattern.

Related

htaccess generic metjod to redirect path to file name

please help to configure .htaccess for this requirement, I can't find this special case in the existing examples around:
http://localhost/project/x/y
should be rewritten to
http://localhost/project/_x_y.phtml
The typical examples are specified almost to be x and y fixed, I'm asking for a generic solution.
Any help appreciate.
Remember to put it in a .htaccess file (File without a name). Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?http://localhost/project/x/y$
RewriteRule ^(.*)$ http://localhost/project/_x_y.phtml [R=301,L]
I'm not sure that's what you meant.
https://www.whoishostingthis.com/resources/htaccess/

How can I redirect one URL with a query string to another and carry it over?

No matter how hard I search for help with htaccess redirect/rewrite rules, I never seem to find an exact match for my problem. And with htaccess, you can't exactly wing it on the syntax!
Please help me redirect any traffic to:
/blue-widgets?param=value1
Needs to redirect straight to:
/blue-widgets/compare-widgets?param=value1
It's probably obvious but the redirect should not happen to any traffic to:
/blue-widgets (direct)
OR
/blue-widgets?param={any other value}
Thank you and Merry Christmas.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)param=value1(&|$) [NC]
RewriteRule ^(blue-widgets)/?$ /$1/compare-widgets [L,NC,R=302]
Query string is by default carried over to target URL.

Apache htaccess rewrite (Pretty URLs)

I have a sneaking suspicion this is not possible, but figured I would ask regardless.
Is it at all possible to take a URL passed to a server in the form of:
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
And rewrite it to appear as:
http://domain.com/Controller/Action/1/2/3
I am trying to clean up an borderline ancient project to support "Pretty URLs" and I would really like to make the URLs display a bit nicer. I know I could setup a 301 header redirect to the new URL, but I would prefer to avoid that overhead if at all possible.
Any thoughts?
Thanks!
To get
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
To appear as
http://domain.com/Controller/Action/1/2/3
You will need to use %{QUERY_STRING} to capture the query string data. Your .htaccess file will look like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Action=Controller/Action&one=(\d+)&two=(\d+)&three=(\d+)
RewriteRule ^.+ /Controller/Action/%1/%2/%3 [R=301,L]
This will set up a permanent redirect to the new page. You can play around and test .htaccess rewrite rules here: htaccess.madewithlove.be
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?Action=$1/$2&one=$3&two=$4&three=$5 [L,QSA]

Apache .htaccess redirect URL directories containing parameters

Sorry to be the asker of yet another tedious mod_rewrite question but after having made no progress in the last few hours, I thought it was time to ask ;)
I am trying to redirect URLs like these:
/some/thing?a=1 --> http://something-else.com/blah
/some/thing?a=1&b=whatever --> http://something-else.com/blah2
No need to keep the param values - the new URL will be hard-coded for each one I have to be redirected.
Have tried a few different things from other posts but with no joy so I am back to square one so any suggestions would be most welcome.
Thanks! :)
You can use the following rule-set:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^a=1$
RewriteRule /some/thing http://something-else.com/blah [L]
This is indeed quite a common question, and people tend to overlook the QUERY_STRING variable. Have you tried it before?
This is what I used in the end:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(a=1&b=whatever)$
RewriteRule /some/thing http://something-else.com/blah2 [L,R=301]
I was adding the rules at the bottom rather than directly below the "RewriteEngine On" which was preventing it from working.
This solution does still append the params to http://something-else.com/blah2 which isn't exactly what I wanted but it will do.

mod_rewrite to change my urls

I've been fighting with mod-rewrite for a while.
Basically, I have a website that I'm moving to a difference namespace/directory.
What I'd like to do is change urls that look like this:
http://mydomain.com/index.php?a=xxxxxxxxxx
These urls will always have "index.php?a=". I have a different/new site that also has an index.php file, so it's important that I do a rewrite only when a= is in the URL.
The new url should be like
http://mydomain.com/ns1/index.php?a=xxxxxxxxxxx
Seems pretty simple, but i can't seem to get mod_rewrite to do it for me, here's what I have:
# rewrite old urls to new namespace
RewriteRule ^/index.php\?a=(.*)$ /gc1/index.php\?x=1&a=$1 [R=301,L]
See anything wrong?
You can use this rule to add a prefix to the path:
RewriteRule !^ns1/ /ns1%{REQUEST_URI} [L]
This rule prefixes the URI path with /ns1 if the path is not already starting with it.
I believe that RewriteRule is only valid for paths, (I.e. it won't take into account any query parameters that you add.)
You are probably better off doing this in the PHP file itself.
// On old site
if($_GET['a']=="xxxxxxx"){
header("Location: /ns1/index.php?a=".$_GET['a']);
die();
}
Thanks to both of you.
I was able to do it:
# rewrite old urls to new ns1 namespace
RewriteCond %{QUERY_STRING} ^a=.*$
RewriteCond %{REQUEST_URI} !^/ns1/.*$
RewriteRule ^(.*)$ ns1$1 [R=301,L]