www to non www redirecting issue - php

I am using .htaccess to redirect from www.test123.com to non http://test123.com site.
Because it cause the cross domain issue in Ajax.
I am not able to use www.test123.com in Ajax(returns 500 error).
So, i used this code in .htaccess file
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
And all thing is working great.
Now issue is, i have unity app which calling php file like this
http://www.test123.com/save.php
And posting some variables with this URL also.
so i am not able to get that post data because this will redirect from www.test123.com to http://test123.com. So i lost post data.
What should i do to get my post data?

You should ignore POST method from your redirect rule as POST data does get lost while doing a full redirect.
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]

Related

.htaccess to rewrite static page to another page

I want www.example.com/blog.php to redirect to www.example.com/blog.php?post=welcome without breaking the .php script.
Simply redirecting /blog.php to /blog.php?post=welcome will break the file and return a page loading error.
I want /blog.php to immediately redirect to /blog.php?post=welcome, and want all the functions of blog.php to work as is.
I tried .htaccess rewrite code but its not working.
RewriteEngine On
RewriteCond %{HTTP_HOST} !blog.php$ [NC]
RewriteRule ^(.*)$ blog.php?post=welcome [L,R=301]
Please help with the rewrite code.
Thanks
You can use:
RewriteCond %{QUERY_STRING} ^$ [NC]
RewriteRule ^blog\.php$ %{REQUEST_URI}?post=welcome [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^$ [NC] ensures that redirect happens only when query string is empty this preventing a redirect loop.
Make sure to test in a new browser.

Redirect all non-www to www except one subdirectory using htaccess.

If want to redirect all non-www requests to my site to the www version. All I need to do is add the following code to my .htaccess file.
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule .* http://www.mydomain.com%{REQUEST_URI} [R=301,L]
The problem is that when I write for example mydomain.com/products-1 (hidden URL for mydomain.com/products?category=1), all parameters become visible, even though they are specified on the .htaccess file, and I get an output url (after the redirect) of www.mydomain.com/products-1?category=1
How can I fix this? Is there any kind of problems with the .htaccess code above?
Try Changing your RewriteRule:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
I prefer this because it will catch all *.domain.com. If that is not what you want, then use your original HTTP_HOST rule.
If my logic is working this morning, this rule should rewrite any requests that do not match:
www.example.com
and do not contain
/subfolder
to
www.domain.com/URI

htaccess subdomain rewrite to variable without being visible in the url

I need to convert "subdomains" to variables using htaccess.
But I don't want the original url to be displayd after the rewrite.
Here's what I have so far:
rewritecond %{http_host} ^([A-Za-z0-9-]+)\.mysite.\com$ [nc]
rewriterule ^(.*)$ http://www.mysite.com/$1?aff=%1 [nc]
But it doesn't seem to work.
What I need is the following:
User links to:
aff1.mysite.com
The displayed url changes to:
www.mysite.com/
But the actual url that the server reads is:
www.mysite.com/?aff=aff1
I'm also going to be adding languages to the mix, so I might even need something like the following:
User links to: aff1.mysite.com
Then I get the language from a cookie {HTTP_COOKIE} lang=([A-Za-z]+)$
If there's nothing in the cookie, the default should be "en"
Then the user gets redirected to aff1.en.mysite.com
Which should then be displayed as: www.mysite.com/en/
but to the server: www.mysite.com/?lang=en&aff=aff1
Please tell me this is possible, and how I can achieve this. I'm very new to rewriting. And I've viewed other questions and tried their solutions, but somehow can't adapt it to exactly what I need.
Also... www.mysite.com shouldn't go to www.mysite.com/aff=www
It should just go to /mysite.com/en/
So, I've spent way more time than I wanted to on this, but finally got something working. Instead of passing the variables in the url, I'm dropping a cookie (which was ultimately what I was doing on the page anyway, but this cuts out a step - and I didn't know that you could drop cookies with the rewrite).
If anyone has a better solution with less lines of code, please feel free to comment or post a new answer.
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{http_host} ^www.example.info [nc] #if url starts with www.example.info
RewriteRule ^(.*)$ http://example.info/$1 [r,nc] #rewrite without the www
RewriteCond %{http_host} ^www.(.*).example.info [nc] #if url starts with www.(aff).example.info
RewriteRule ^(.*)$ http://%1.example.info/$1 [r,nc] #rewrite without the www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{http_host} ^(.*)\.example.info [nc] #if subdomain is aff
RewriteRule ^(.*)$ - [co=aff:%1:example.info:7200:/] #drop/update cookie with aff
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{http_host} ^(.*)\.example.info [nc] #if subdomain is aff
RewriteRule ^(.*)$ http://example.info/$1 [nc,R,L] #rewrite without subdomain

redirecting-to/adding www in url codeigniter based project

I am trying to add www and redirect all non-www urls to www urls in a codeigniter-based project, but my post data is not working after doing so. Here is the content of the Apache .htaccess file:
RewriteCond %{HTTP_HOST} ^examplesite\.com$ [NC]
RewriteRule ^(.*)$ http://www.examplesite.com/$1 [R=301,L]
I have $config['uri_protocol'] = 'AUTO';.
Everything works like a charm if I do not have www urls. I have tried REQUEST_URI, but it didn't work either.
This is because browsers wont resubmit POST data if the response is a redirect. This is a normal and expected thing. What you can do is to only rewrite GET requests with an extra condition:
RewriteCond %{REQUEST_METHOD} GET [NC]
RewriteCond %{HTTP_HOST} ^examplesite\.com$ [NC]
RewriteRule ^(.*)$ http://www.examplesite.com/$1 [R=301,L]
Leave the $config['site_url'] option in application/config/config.php to an empty string (that will make CI do autodetect) and in your view code, use the site_url() helper or the form builder functions so it will use the domain that the request came in.

Rewrite subdomain but keep url

I have a website at domain.com, it has a login page at domain.com/user/login.
I would like login.domain.com to show the login but still have the url login.domain.com.
what i have now:
RewriteCond %{HTTP_HOST} ^login.domain.nl$ [NC]
RewriteRule (.*) http://domain.nl/user/login$1 [L]
but this changes the url as well and if i change it to:
RewriteCond %{HTTP_HOST} ^login.domain.nl$ [NC]
RewriteRule (.*) http://domain.nl/user/login$1 [P,L]
i get a 400..
what am i doing wrong?
This question is in the wrong seciton, however, you can't necessarily call the remote page like you want to (even using the proxy flag), you need to make a local reference.
You need to do something like:
RewriteCond %{HTTP_HOST} ^login.domain.nl$ [NC]
RewriteRule (.*) /user/login.php?arguments=$1 [QSA,L]
That references the local location of the file.

Categories