Haven't found what i'm looking for so far.
I want to redirect all my site visitors to the secure version of the URL they type in, so i've put this in my .htaccess-file.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
So far so good, but now I want that visitors who type in
www.mywebsite.com/directory/param1/etcetera/ or
www.mywebsite.com/directory/file.php?param1=value1
are being redirected to
https://www.mywebsite.com/directory/param1/etcetera/ or
https://www.mywebsite.com/directory/file.php?param1=value1
as well, cause that isn't happening.
Can some guru help me out? :) thnx
Your first case, www.mywebsite.com/directory/param1/etcetera/, should be redirected to its https variant by your RewriteRule. I can't see why that wouldn't work.
To cover the second case, in order to have Apache include the query string, you need to add the QSA flag, so:
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L,QSA]
Related
I have a url as below.
http://example.com/listing?listingid=237508&2000
What I want is to redirect it to a mobile version of the site.
http://example.com/mobile-listing?listingid=237508&2000
The problem is that if it is example.com/listing it should redirect to example.com/mobile-listing. If it is example.com/event it should redirect to example.com/mobile-event and so on. How am I to do this? I have seen question about redirection to mobile version based on url parameters or getvalue. But not this way. Any help would be greatly appreciated. Thanks.
i've found a simuluar answer from ulrich palha which might helps.
question id 9113747
in the .htaccess file of root you can try this:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)listingid= [NC]
RewriteRule ^ mobile-listing [L,R=301]
hopes this helps. you might change a little and this should point in the right direction to the solution.
You need to use a RewriteRule directive and QSA and PT flags
<VirtualHost *:80>
RewriteEngine on
RewriteRule ^/listing$ /mobile-listing [PT,QSA]
…
</VirtualHost>
Flags
PT:
Forces the resulting URI to be passed back to the URL mapping engine
for processing of other URI-to-filename translators, such as Alias or
Redirect. details ...
QSA:
Appends any query string from the original request URL to any query
string created in the rewrite target.details
More information
You can find more scenarios at Redirecting and Remapping with mod_rewrite.
Without any sort of condition for what is mobile or what isn't, and you always redirect, then the rule would look something like this:
RewriteCond %{REQUEST_URI} !^/mobile-
RewriteRule ^([^/]+)$ /mobile-$1 [L,R]
The query string will automatically get appended. Change the R to R=301 if you want to permanently redirect.
I have limited .htaccess knowledge and requires some help. I need to redirect all page request to www.newdomain.com except for www.olddomain/page.json but the query string get dropped when it redirect. how can i preserve it? thankyou very much!
Current code:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/page.json
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]
Edit: Just to make it clear, i only need to stay on page.json on the old domain, lets say user request for www.olddomain.com/page1.json?session=gVgr30ExUlM
i need to redirect to
www.newdomain.com/page1.json?session=gVgr30ExUlM
BUT when it is www.olddomain.com/page.json?=LKJHGF i need it to stay on that old domain and wont redirect is it possible?
Use the [QSA] flag ("query string append")
You need to add [QSA] and optionally [NE] which should give you:
RewriteRule (.*) http://newdomain.com/$1 [QSA,NE,R=301,L]
I want to redirect website user to www.mywebsite/users.php?user=xyz when the user types http://xyz.mywebsite.com into the address bar where http://xyz.mywebsite.com is virtual and doesnt exist.
I am a beginner and doesnt know much about url rewriting. Tried to search google and stack overflow but didnt got the solution.
I started with this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+).mobilehealthnig.com$
RewriteRule (.*) http://www.mywebsite.com/users.php?subdomain=%1
</IfModule>
also tried this but none of these are working for me
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mywebsite.com
RewriteCond %{HTTP_HOST} ([^.]+).mywebsite.com
RewriteRule ^(.*)$ /users.php?subdoamin=%1
Ok, based on your additional input I suggest this setup:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.mywebsite\.com$
RewriteRule (.*) http://www.mywebsite.com/users.php?subdomain=%1 [L]
</IfModule>
If that does not work (I take that from your comment), you have to check where the actual problem is:
does rewriting work at all?
does this rule do anything atall? what?
is that rule ignored? why?
What absolutely makes most sense in such cases is to turn on rewrite logging to see and understand what is actually happening inside the rewrite engine. Please take a look at the two commands RewriteLog and RewriteLogLevel inside the manual mentioned. Enable a log level of maybe 7 and look what is logged when you make a single request. It should give you an idea of where to look for the cause of the problem.
I have a requirement where in I want to give users of my site their personal url.
Something like "http://abc.example.com" and when any user types this url in browser it should open this link "http://www.example.com/index/sub-domain?username=abc"
So I tried writing and trying many codes and finally was successful with below code but problem is it redirects. I want an internal redirection. URL address window should remain as "http://abc.example.com".
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*$ index.php [NC]
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} ^([a-z]+)\.example.com$
RewriteRule ^(.*)$ http://www.example.com/index/sub-domain?username=%1
</IfModule>
I am not sure if It is possible or not ? Any advice or help will be of great help. Also can anyone suggest me some .htaccess tutorial.
If you do not specificy the R flag on your RewriteRule, mod_rewrite normally performs an internal rewrite. However, since you are using an absolute URL as your rewrite target, it has to be the exact same host, otherwise an external redirect will be issued.
If you really wish to internally redirect to another host, you should check out mod_proxy.
How to redirect a first time user to a special landing page using htaccess based on referrer? I mean if they came from another domain then they are the first time visitor?
I am really noob at url rewriting and explanation would be great .
Note: the landing page is nothing but a php script that detects browser. On that page I will use cookie, but need to redirect the user if the referrer is empty or its from another domain.
I suggest this :
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?(https?://)?example\.com[NC]
RewriteCond %{REQUEST_URI} !^/welcome.html [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,L]
The first RewriteCond check if referer contains your domain name, and the second check if you are not just redirected by the RewriteRule.
The RewriteRule brings you to the welcome page as a [L]ast rule.
How about redirect the use if his referer is not your domain ?
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]
That means that the user will be redirected to welcome.html if he writes example.com in the address bar or comes from a link in another site. Once on your site it won't be redirected anymore if he load another page in your site.
P.S. AFAIK you can use cookies in PHP that generates a plain html page see here
Edit: Update tested code
Excuse my reheating the old steak once more.. I would still be interested in knowing if anyone knows the solution to this problem - without using cookies or HTML5 features...
I have read here that the HTTP_REFERER might be blank. Is that why this method of redirecting is not good for this application? I have experimented with this on my server but the closest result working result was being always redirected to my landing page index.htm, which is not desired..
Could this rule interfere with other rewrite rules?
Also, there is an error in the former snippet:
And I think the NC flag in the latter snippet does not make sense. Should it not be L?
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?(https?://)?example\.com[NC]
#missing space after .com and before [----------------here----^
RewriteCond %{REQUEST_URI} !^/welcome.html [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,L]
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]
#Should this flag not be L? ------------------------------^