I need some help with writing RewriteCond in 1 string correctly for:
www.mydomain.com/?q=home (both https and http)
Important:
www.mydomain.com/administration/?q=home
etc and
www.mydomain.com/?q=home&z=123
etc should NOT be proccessed. Only
www.mydomain.com/?q=home
The below 2 rewriteconds, should process only for www.mydomain.com/?q=whateverstring
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^q=([^&]+)$ [NC]
add whatever RewriteRule you want after the above.
Try adding the following to the .htaccess file in the root directory of your site.
This will work for http/s
RewriteEngine on
RewriteBase /
#if the request only has q=home
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?q=home\ [NC]
# then add Rewrite rule here
Related
I have server with domain www.domain.com and multiple sub domains sub1.domain.com, sub2.domain.com. They are all pointing to server root.
I'd like users to access specific folders by subdomains. For example:
sub1.domain.com/someURI => sub1.domain.com/subFolder1/someURI
sub2.domain.com/someURI => sub1.domain.com/subFolder2/someURI
I would like to hide these redirections from users. I tried following .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub1.domain.com$
RewriteRule ^(.*)$ http://sub1.domain.com/subFolder1/$1 [R,L]
RewriteCond %{HTTP_HOST} ^sub2.domain.com$
RewriteRule ^(.*)$ http://sub2.domain.com/subFolder2/$1 [R,L]
It is redirecting correctly only without any URI and redirection is visible.
You have 2 issues that are causing external redirect:
Using R flag in RewriteRule
Using Absolute URL starting with http:// in target
Another issue is that your rewrite rule is unconditional which can cause infinite looping.
You can use these rules:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} =sub1.domain.com
RewriteRule ^((?!subFolder1/).*)$ subFolder1/$1 [NC,L]
RewriteCond %{HTTP_HOST} =sub2.domain.com
RewriteRule ^((?!subFolder2/).*)$ subFolder2/$1 [NC,L]
The following portion below is written into my .htaccess file. I'm using the ZenCart "CEON URI Mapping" module. I'm having a problem as there is a part of a PHP script that looks for a certain variable and displays a CSS display box when found.
What I need to happen is for htaccess to NOT rewrite URL's with a "amznPmtsOrderIds" GET parameter when set.
How would I incorporate that into the following:
# BEGIN CEON URI MAPPING
# Don't rewrite any URIs ending with a file extension (ending with .[xxxxx])
RewriteCond %{REQUEST_URI} !\.[a-z]{2,5}$ [NC]
# Don't rewrite admin directory
RewriteCond %{REQUEST_URI} !^/admin [NC]
# Don't rewrite editors directory
RewriteCond %{REQUEST_URI} !^/editors [NC]
# Don't rewrite cPanel directories
RewriteCond %{REQUEST_URI} !/cpanel [NC]
RewriteCond %{REQUEST_URI} !/frontend [NC]
RewriteCond %{REQUEST_URI} !\.mp4$ [NC]
RewriteCond %{REQUEST_URI} !/livehelp.*$ [NC]
RewriteCond %{REQUEST_URI} !/rest.php/
RewriteCond %{REQUEST_URI} !/restInterface\.php.*$
# Handle all other URIs using Zen Cart (index.php)
RewriteRule .* index.php?%{QUERY_STRING} [L]
# END CEON URI MAPPING
Thanks for the assistance.
Immediately before this line ...
# Handle all other URIs using Zen Cart (index.php)
... add this:
# Don't rewrite any URIs with a "amznPmtsOrderIds" GET parameter
RewriteCond %{QUERY_STRING} !amznPmtsOrderIds [NC]
I have this link: http://www.domain.com.mk/lajmi.php?id=2790,
and i want to change it to http://www.domain.com.mk/lajmi/2790
With this code I can change the link to /lajmi/2790 but i get 404 error.
I mean i get the link
http://www.domain.com.mk/lajmi/2790, but it has 404 error (i dont se the content)
This is my code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
What I am doing wrong ?
Try this one :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1&r=0 [L]
(the &r=0 in the final rule is for not getting an infinite loop)
Single direction rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L,QSA]
This means that every uri of kind /lajmi/2790 will be passed to /lajmi.php?id=2790 in a sub-request.
However, in this case, if the user hits /lajmi.php?id=2790 by himself, then this is the url he will see in the browser, not the "beautified one".
Bi-directional rewrite:
RewriteEngine On
RewriteBase /
; Redirect lajmi.php?id=2790 to a beutified version, but only if not in sub-request!
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{IS_SUBREQ} !=true
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ lajmi/%1 [R=301,L]
; Make the beutified uri be actually served by lajmi.php
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L]
Here, an additional RewriteCond was added to the first rule checking that this is not a sub-request, to ensure that the rules do not loop.
You can pick which way you like, but the first approach is enough if you build the links in your HTML in the 'beautified' way already (no need to redirect the browser twice just to see the page).
is it possible to have an htaccess rule that will redirect my files from
http://www.mydomain.com/page.html to http://cdn.mydomain.com/page.html but still making the link look like http://www.mydomain.com/page.html
I know masking urls isn't possible, but since they are on the same domain i was wondering if that was possible
Try these rules in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for http
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://cdn.mydomain.com/$1 [L,R]
# for https
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://cdn.mydomain.com/$1 [L,R]
However one caveat that it is an external redirect hence URL in your browser will change to http://cdn.mydomain.com/foo because when you are jumping from one host to another you cannot have internal redirect hence R flag is needed.
No idea about .htaccess but you could use a curl script in PHP.
I have subdomains: sub1.tld.com, sub2.tld.com and so on. This subdomains contain important info. But I want to prevent opening any other urls, for example: sub1.tld.com/anypage or sub2.tld.com/dir/more/anyurl.html. This urls must be redirected to main domain tld.com: tld.com/anypage or tld.com/dir/more/anyurl.html
So, i have to check 2 things before redirection:
it's any subdomain
it's not subdomain's root page
I created this rules:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*) http://tld.com/$1 [R=301,L]
This set of rules works as expected (redirects sub1.tld.com/anypage to tld.com/anypage) but also redirects sub1.tld.com to tld.com. Where is the error? 2nd string is wrong?
Try this:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com$ [NC]
RewriteRule ^/(.+)$ http://tld.com/$1 [R=301,L]
Or you can use this string to check if it root:
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /\ HTTP/
For some unknown reason the most reliable for checking variable was %{THE_REQUEST}, not %{REQUEST_URI}.
%{THE_REQUEST} contains raw browser's query to web-server: for example, GET /anypage.html HTTP/1.1, if you are opening url http://anysite.com/anypage.html.
So, here you are complete working rule-set for .htaccess:
RewriteCond %{HTTP_HOST} ^.+\.tld\.com$
RewriteCond %{THE_REQUEST} ^get\ /(.+)\ http.* [NC]
RewriteRule ^(.+) http://tld.com/$1 [R=301,L]