I have the following rules in my httpd.conf
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} password=*
RewriteCond %{QUERY_STRING} bi2=(.*)
RewriteCond %{REQUEST_URI} /myGet.php(.*)$
RewriteRule ^(.*)$ http://blog.myexample%1.com/$1
However, when I executed the Request URI
/myGet.php?password=john&bi2=67
I was redirected to
http://blog.myexample.com/myGet.php?password=john&bi2=67
instead of
http://blog.myexample67.com/myGet.php?password=john&bi2=67
It seems that %N for the RewriteCond BackReferencing is not working. Although $N is.
Alter the order or the RewriteCond directives so that the last directive is the one you want the information from:
RewriteCond %{QUERY_STRING} password=*
RewriteCond %{REQUEST_URI} /myGet.php(.*)$
RewriteCond %{QUERY_STRING} bi2=(.*)
RewriteRule ^(.*)$ http://blog.myexample%1.com/$1
But I rather suggest:
RewriteCond %{QUERY_STRING} password=
RewriteCond %{QUERY_STRING} bi2=([^&]*)
RewriteRule ^/myGet\.php.*$ http://blog.myexample%1.com$0
Related
I would like to check if file exist in subdirectory.
And I mean subdirectory that "is taken" from "subdomain".
Eg. url: "test.domain.com/file.txt" file location is "/test/file.txt"
I have wildacrd and i can't hardcode "test" directory. How can i do it?
I tried add %{DOCUMENT_ROOT}%1/$1 but this dosen't work.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com
RewriteCond %{DOCUMENT_ROOT}%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}%1/$1 -d
RewriteRule ^ /%1%{REQUEST_URI} [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com
RewriteRule ^images/(.*)$ /%1/image.php?test=$1 [L]
With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com [NC]
RewriteCond %{DOCUMENT_ROOT}/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1/$1 -d
RewriteRule ^(.*)/?$ %1/$1 [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com [NC]
RewriteRule ^images/(.*)/?$ %1/image.php?test=$1 [QSA,NC,L]
Fixes in OP's attempts: You need not to use condition RewriteCond %{ENV:REDIRECT_STATUS} ^$ in 2nd Rules sets, even first one may not need it but for safer side you can have it. Then your sequence of accessing path from %{DOCUMENT_ROOT} was not correct which was corrected here.
I need to get a redirect using htaccess
From: http://example.com/go/zaym?source=`PARAM1`&keyword=`PARAM2` but if there are no URL params I need also to get redirect with empty params
To: another domain http://example2.com/sub_id=PARAM`&sub_id2=PARAM2
So I need to move value source to sub_id and value keyword to sub_id2
I'm trying so, but it's not working:
RewriteCond %{QUERY_STRING} ^source=([^&]+)&keyword=([^&]+)$
RewriteRule ^go/zaym http://example2.com/sub_id=%1&sub_id2=%2 [R,L]
EDIT(More generic way): With more generic way as per OP's comment one could try following, where no hard coding of keyword or source strings, then try following.
With only query string condition check:
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^[\w-]+=(.*)(?!=&)$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1 [NE,L]
RewriteCond %{QUERY_STRING} ^[\w-]+=([^&]*)&[\w-]+=(.*)$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1&sub_id2=%2 [NE,L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteRule ^ http://example2.com%{REQUEST_URI} [L]
With URI + query string condition:
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^[\w-]+=(.*)(?!=&)$ [NC]
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1 [NE,L]
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteCond %{QUERY_STRING} ^[\w-]+=([^&]*)&[\w-]+=(.*)$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1&sub_id2=%2 [NE,L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteRule ^ http://example2.com%{REQUEST_URI} [L]
Why does this rewrite rule not work?
RewriteCond %{HTTP_HOST} domain.(se|dk)/shop/$ [NC]
RewriteRule ^ https://domain.$1 [R=302,L,NE]
I am attempting to catch all requests to domain.dk/shop/ and domain.se/shop/ and redirect them to domain.dk, or domain.se.
What seems to be the issue?
Entire htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# Redirect everything to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} domain.(se|dk)$ [NC]
RewriteRule ^shop$ https://domain.%1/ [R=302,L,NE]
</IfModule>
The HTTP_HOST doesn't have any path information, it is only "domain.se". The path information must be in the rule's regex match:
RewriteCond %{HTTP_HOST} domain.(se|dk)$ [NC]
RewriteRule ^shop$ https://domain.%1/ [R=302,L,NE]
Also, you need to use the %1 backreference, which references a grouping in the condition. The $1 backreference is one from the rule's grouping (which doesn't have one).
I'm trying to rewrite some legacy Joomla URLs on a site that's now using ExpressionEngine as its CMS but they're not working.
The ExpressionEngine URL rewrites, i.e. removing index.php from the URL work fine though.
This is what I've got:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This is the one that's not working
RewriteRule /index.php?option=com_chronocontact&Itemid=54 /contact/ [R=301,L]
# Force www
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Redirect index.php Requests
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
RewriteCond %{REQUEST_URI} ^/
RewriteCond %{QUERY_STRING} ^(gclid=.*)
RewriteRule ^(.+) /index.php?/ [L,PT]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(assets|css|images|tinymce|js|min|cms|themes|index\.php|admin\.php|favicon\.ico|index\.php|path\.php|php\.ini) [NC]
RewriteRule ^(.+) /index.php?/ [L]
</IfModule>
Can anyone spot what I'm doing wrong?
The first thing is the stray RewriteCond %{HTTPS} !=on that you have at the top. It looks like it belongs to the rule under it, as in:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
As far as the rule that you have commented that doesn't work, the ? is a reserved character for regular expressions, and your pattern actually says that the second p in /index.php is "optional". Additionally, you can't match against the query string in a rewrite rule, you need to use a rewrite condition and match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^option=com_chronocontact&Itemid=54$
RewriteRule ^(index.php)?$ /contact/? [R=301,L]
is probably more along the lines of what you're looking for.
I need to create a rule for all GET requests to my site so that:
GET /articles/topic1.html?showall=1
is rewritten as:
GET /articles/topic1.html
I have the following .htaccess file on my Joomla site:
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
########## This is the Rule I have added ################
RewriteRule ^(.*)$ /$showall=1 [R=301,L]
RewriteBase /
I have added comments on the line that I have added. Unfortunately it doesn't work, the browser warns me I have generated a circular loop....
Any idea how to fix it ??
Thanks
Frank
Try replacing that with:
RewriteCond %{QUERY_STRING} ^showall=([0-9])+$
RewriteRule ^articles/topic[0-9]*\.html /articles/topic%1.html? [L]
Or if it's really topic1.html, then you want this:
RewriteCond %{QUERY_STRING} ^showall=1$
RewriteRule ^articles/topic1\.html /articles/topic1.html? [L]