I'm trying to do a very simple redirect. We're moving from a standalone Wordpress install to a Multisite Wordpress install, and I'm trying to get the redirect working for a page with query strings. Here's the code I have so far:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^advise/index_pt(.*)$ http://domain.com/advise/pennies-to-millions/$1 [NE,L,R=301]
Basically, this is what I want: (with or without www)
*.domain.com/advise/index_pt.php?(parameters) => *.domain.com/advise/pennies-to-millions/?(parameters)
Any help is appreciated!
Query strings aren't part of the URI used to match in a RewriteRule directive. So you can't use a regular expression to match against them inside a RewriteRule. If you needed to match against them, you can use a RewriteCond %{QUERY_STRING} <regex> before the rule. However, query strings are automatically appended anyways if you don't create any new ones by using a ? in the rule's target. You can remove all the backreferences in the rule and add parens around the www to make them optional.
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^advise/index_pt.php$ http://domain.com/advise/pennies-to-millions/ [NE,L,R=301]
Related
So currently I have URLs that looks like this:
http://localhost/?v=register
http://localhost/?v=profile&u=thatgerhard
I want the above to look like this:
http://localhost/register/ (without trailing /)
http://localhost/profile/thatgerhard/ (without trailing /)
I spent a ton of time trying to get this to work even though it seems like it should be a simple fix.
This is what I have atm:
RewriteBase /
RewriteEngine On
RewriteRule ^((.)*)$ /?v=$1&p=$
I would ideally like this to be dynamic so that if you do ?v=foo it will automatically do /foo/ without adding "foo" to the htaccess file.
Any help or direction will be greatly appreciated. :)
You should use RewriteCond Directive and RewriteCond backreferences. The %1 backreference matches the query string parameter v, the %2 backreference matches the query string parameter u. The RewriteRule redirects the request permanently and discard the query string entirely.
RewriteCond %{QUERY_STRING} ^v=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^v=([^&]*)&u=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/%2/? [R=301,L]
How do you redirect a subdomain whilst capturing anything after the '?' and appending to a redirect using htaccess, whilst maintaining a particular variable?
redirect from...
http://subdomain1.example.com/?retailcentre=subdomain1&utm_source=twitter&utm_medium=twitter&utm_campaign=022641example
to...
http://www.example.com/?retailcentre=subdomain1&utm_source=twitter&utm_medium=twitter&utm_campaign=022641example
The query string after the '?' could be anything as it could come from twitter, facebook etc.
The one var that has to be parsed is what subdomain the redirect came from, in this case 'retailcentre'.
Try with:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI}?retailcentre=subdomain1 [QSA,NE,R=301,L]
[QSA] Flag: When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
You can use the following rules :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{THE_REQUEST} /\?retailcentre=.+&utm_source=.+&utm_medium=.+&utm_campaign=.+\sHTTP [NC]
RewriteRule ^ http://example.com/ [L,R,NE]
I use the following rules to rewrite a subdomain call to a page in the root of the website:
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9/_.-]+)\.domain\.com$ [NC]
RewriteRule .? headlines.php?url=%1 [L,QSA]
This works fine. I use this for a rss - news related website. For example: http://economy.headlines.com will internally look at http://www.headlines.com/headlines.php?url=economy
I also want to link to the news items in the following way:
economy.headlines.com/news/title/id
How do i do this ? Because every time the first rules are "fired". Even if i make other rules with the [L] flag the other rules are fired and nothing happened.
How can i combine the rules above with new rules which also look at files in the root of the site but with parameters in the url ?
You should be able to combine both if you evaluate the path component of the request you rewrite:
Rewriteengine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9/_.-]+).domain.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ headlines.php?url=%1&category=$1&title=$2&id=$3 [L,QSA]
Probably you have to do some fine tuning, but I think you get the idea: the first argument of the RewriteRule is a regular expression that splits the request path into its components. Those can be referred to using the $1 and $2 notation you can see towards the end of the rule.
Edit: added the category parameter to the RewriteRule as discussed in the comments below.
I've been trying to rewrite a URL such as
www.somesite.com/?x=372
into a url
www.somesite.com/
My current code does not seem to work
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule http://www.somesite.com/ [R=301,L]
I've looked up countless ways of trying to do it with htaccess and still no success.
If you simply want to redirect a client to remove the query string (everything after the ? in the URL), then you can try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule ^ http://www.somesite.com/? [R=301,L]
You've got most of it right, it seems, but your rule needs to have a match, and your target (the http://www.somesite.com/) needs a ? at the end so that any query string before the rewrite won't get appended.
In Apache 2.4 or newer you can use the QSD query string discard flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule .* http://www.somesite.com/ [R=301,L,QSD]
I have a url which ends with a certain variable string, and was erroneously generated and indexed unfortunately.
Example:
http://domain.com/anything-in-between/?var=xyz-abc-abc-abc
How can I redirect to main site (kill it), by detecting 'abc-abc-abc' using htaccess?
Why wouldn't this work and what would be the best solution:
RewriteCond %{REQUEST_URI} abc-abc-abc
RewriteRule .* index.php
You want to use the query string as claesv suggests but you need to then kill the query string
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} \bvar=.*?abc-abc-abc$
RewriteRule ^ index.php? [L]
This will do it silently (i.e. in the server as an internal redirect and not involving the browser). You can't use 301s reliably to trim query strings.
Something along the lines of:
RewriteCond %{QUERY_STRING} ^var=.*abc-abc-abc$
RewriteRule ^.*$ http://domain.com/ [R=301,L]