I have a simple htaccess redirect to send visitors from website.com/promotions to website.com/go/promotions. If I use the code below I get a redirect loop error:
RewriteEngine on
RewriteCond %{REQUEST_URL} !^go$
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC]
The RewriteCond is supposed to prevent the loop by not redirecting if the request url contains "go" but it doesn't appear to be working.
In addition I'm trying to set up a regex so that promotion/anypage.html will be redirected to go/promotion/anypage.html. I tried the following code but it wouldn't match anything:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^go$
RewriteRule ^promotion/(.*)$ go/promotion/$1 [R,L,NC]
Does anybody have an clues as to why either of these blocks isn't working properly ?
Cheers
%{REQUEST_URL} isn't a var you can match against, you're probably thinking of %{REQUEST_URI}, but it starts with a leading slash so ^go will never match anything.
You just need a single rule:
RewriteEngine On
RewriteRule ^/?promotion/(.*)$ /go/promotion/$1 [R,L]
Or you can use mod_alias:
Redirect /promotion/ /go/promotion/
You're checking if the entire URL is just "go" on it's own, which it's not:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?go/
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC]
Related
I am trying to achieve a simple redirect - from /news to /insights
I have the following in my .htaccess file:
redirect 301 /news /insights
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
Whenever I've used the redirect directive before, any matched URLs would be redirected and no further rewrites in the file would be processed. That is to say, going to /news would send you to /insights, and the rewrite to index.php would not be processed.
However, with this current setup, going to /news sends me to /insights?p=news, so for some reason the rewrite to index.php is still being processed.
Furthermore, if I comment out the index.php rewrite, then I get sent to /insights as expected.
This isn't how I've usually experienced this working so am unsure why it's doing this.
I have also tried the following:
RewriteEngine On
RewriteRule "^/news" "/insights" [R=301,L]
This simply results in a 404 instead of redirecting, which I also do not understand.
I am aware I could do the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} "^/news"
RewriteRule ^ /insights [R=301,L]
which does work, however, I don't really want to have multi-line rewrites for lots of URLs, and would like to understand why the other 2 examples do not work.
You just need to insert this rule before last catch-all rule.
RewriteRule ^/?news/?$ /insights [R=301,L,NC]
Place it just below RewriteEngine On line so that mod_rewrite engine executed this rule before other rule.
Make sure to test it in a new browser.
This is super simple but it's driving me crazy! I have a website at http://example.org/ and a subdirectory at http://example.org/ccc/
I want to redirect anything outside of the /ccc/ directory to a different website.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/ccc/?.*
RewriteRule ^(.*)$ https://new-website.com/$1 [L]
But this code doesn't work, it redirects the /ccc/ directory. According to my research and testing with this htaccess tester, it should not redirect because the RewriteCond is checking against /ccc with optional slash and other characters after it.
What is happening? Does this look correct?
Edit: This method from this answer is also not working, the CCC domain is being redirected:
RewriteEngine on
RewriteRule ^ccc index.php [L]
RewriteRule (.*) https://new-website.com/$1 [R=301,L]
PHP 5.4.45, Apache/2.2.31
Assuming ccc/ directory doesn't have a separate .htaccess, you may use this rule:
RewriteEngine on
RewriteCond %{THE_REQUEST} !\s/ccc[/?\s] [NC]
RewriteRule ^ https://new-website.com%{REQUEST_URI} [L,R=301,NE]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. An example value of this variable is GET /index.php?id=123 HTTP/1.1
It looks like [L] isn't behaving normally and I'm guessing it's the old version of Apache (2.2.31) because these rules worked on a separate website. I found this solution which seemed to work for this case, the third line below:
RewriteEngine on
RewriteRule ^ccc/? index.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} != 200
RewriteRule ^(.*)$ https://new-website.com/$1 [L]
Explanation from that question:
The problem is that once the [L] flag is processed, all the next RewriteRules are indeed ignored, however, the file gets processed AGAIN from the begin, now with the new url.
This magic Condition will not process the catch all if the file was already redirected.
I'm using magento and every url is acessible in two ways for example:
http://www.mysite.com/product-item, and
www.mysite.com/product-item?___store=default
I tried a regular 301 redirect in .htaccess but it won't redirect. Does anyone have the code that would automatically redirect all url's with the suffix ?___store=default back to the clean url's thus avoiding duplicate content issues?
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?___store=default
RewriteRule ^(.*)$ /$1? [L,R=301]
or
RewriteEngine On
RewriteCond %{QUERY_STRING} ___store=default
RewriteRule ^(.*)$ /$1? [L,R=301]
depending on how the query string is generated. The reason why a Redirect 301 probably didn't work is that you can't match against query strings using that statement.
I have this dynamic link:
http://www.nortedigital.mx/article.php?id=36175&t=dobla_las_manos_el_snte__avala_reforma_educativa
and I need to convert in URL friendly like this:
http://www.nortedigital.mx/36174/se_enriquecio_elba_en_sexenios_del_pan.html
and i have this RewriteRule:
RewriteRule ^([^/]*)/([^/]*)\.html$ /article.php?id=$1&t=$2 [L]
but doesn't work. Please, anybody can help me?
You must capture the query string in a RewriteCond and use that in the RewriteRule
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=(.+)$
RewriteRule ^/?article\.php$ /%1/%2.html? [R,L]
This redirects the client to request i.e. /36174/se_enriquecio_elba_en_sexenios_del_pan.html. Now you must server the real page. For that, we add an additional rule, similar to the one you already have in your question
RewriteRule ^/?(.+?)/(.+?)\.html$ /article.php?id=$1&t=$2 [L]
But now, there's an endless redirect loop. We break this by using an environment variable. Here is the whole complete ruleset
RewriteEngine On
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=(.+)$
RewriteRule ^/?article\.php$ /%1/%2.html? [R,L]
RewriteRule ^/?(.+?)/(.+?)\.html$ /article.php?id=$1&t=$2 [L,E=SEO:1]
This rule does the redirect as above, as long as the environment variable is not set. And it serves the real page from article.php and sets the environment variable at the same time to prevent the loop.
You can use cookies for this purpose too. But that will break, if cookies are disabled in the client.
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]