http to https without URL changing - .htaccess

I need some help with .htaccess as I am very new at this..
Here is my situation:
I want to have a short url http://example.com/shorturl point to https://example.com/apps/appname (non-ssl to ssl) but I want to make sure the URL doesn't change in the browser.
now, at example.com/apps/appname level, I have iis url rewrite rule set up so all http traffic is redirected to https and it works as it should.
to achieve my goal i spent a few good hours trying to figure out by researching online but couldn't get it to keep the shorturl. No matter what I tried it keeps changing the URL in the browser to https://example.com/apps/appname.
Here is one of many methods i tried
RewriteCond %{REQUEST_URI} (shorturl) [NC]
RewriteRule ^(.+)$ http://example.com/apps/appname [P]
also tried
RewriteRule ^(.+)$ https://example.com/apps/appname [P] but again, it doesn't keep the URL.
does having the http to https redirect set up at the ../../appname level make any kind of difference as oppose to having it configured at the root?
Any help in getting this figured out would be greatly appreciated.
Thanks in advance and hopefully I included all the detail

Related

301 redirect htaccess on a database driven website

I have created a website using IPB which I believe is written in PHP.
i am trying to use a 301 prominent redirect but when ever I try to use any from around the web I get a 500 server error
the broken url is 'http://thereviewforum.com/index.php?/page/index.html/_/monthly-top-10/top-10-free-vpn-service-providers-r17'
and I would like to direct users and search bots to
http://thereviewforum.com/monthly_top_10.html/_/monthly-top-10/top-10-free-vpn-service-providers-r17
again any help with be greatly appreciated!
I think you can do (The following does not work. See below for working example):
Redirect 301 /index.php?/page/index.html/_/monthly-top-10/top-10-free-vpn-service-providers-r17 http://thereviewforum.com/monthly_top_10.html/_/monthly-top-10/top-10-free-vpn-service-providers-r17
Edit:
You are right the above does not work. I guess query strings aren't considered. I should have tested it first. Here is an example that I tested to make sure it worked.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^/page/index.html/_/monthly-top-10/top-10-free-vpn-service-providers-r17$
RewriteRule .? /monthly_top_10.html/_/monthly-top-10/top-10-free-vpn-service-providers-r17 [L,R=301]

How do I do a .htaccess rewrite that masks the forwarded URL?

I have a url that is www.blahblah.com/something
That is a remote service, I don't have anything to do with it.
How can I use .htaccess on my own server and rewrite from www.myurl.com so that the content displayed is all www.blahblah.com/something, but the address bar still reads www.myurl.com
No, this is not possible with foreign urls.
You can, however, do this locally. For example, look at this htaccess file:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^some/test/url$ index.php?some=test&or=url [L]
In this scenario, if you visit www.myurl.com/some/test/url it will show as such on the browser, but your server will actually be running index.php in your document root with the parameters some=test&or=url.
This is only possible for scripts running on your server. You cannot do this on another server/domain. If you try this (eg, by changing index.php?some=test&or=url in the example above to http://www.blahblah.com/something), then apache will just redirect the browser to that url.
htaccess (Apache) makes the connection to the user, and the user is expecting a response from YOUR server. If you try to load content from another server, Apache would have to make that connection, load the resulting HTML or whatever, and pass it back to you. But this gets messy, especially when you get into cookies, SSL, javascript, etc.
My question is: why do you actually need this? I'm not sure I understand why it is a problem if the user's url changes. If it's a service you have no control over, why is it so bad to just send them to it?
You might want to research more about cache servers, or using PHP to to make the http call to the server you want and "pass through" the content, assuming you know beyond a doubt there will be no issues with cookies or SSL or whatever. But again, why not just send them to the proper URL?
Try this:
RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/$1 [P]
It works for me.
Source: http://www.inmotionhosting.com/support/website/htaccess/redirect-without-changing-url
mod_rewrite is the right way.
Make sure it is mod_rewrite is activated in our apache conifiguration.
add to the .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.blahblah\.com$ [NC]
RewriteRule ^(.*)$ http://www.myurl.com/$1 [R=301,L]
RewriteCond defines the condition. In this case if the http_Host is www.blahblah.com
RewriteRule defines what to do. In this case forward to your target domain. $1 is the rest of your URL
More Details you can find here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Can you help me write htaccess that directs others to static html and me to my Codeigniter site?

Here's the problem, I'm doing a rewrite of a Codeigniter site that has the requisite htaccess for removing the index.php.
While doing the rewrite, I want to redirect visitors to a static HTML page while I do the rewrite and test. In other words, I want to access the Codeigniter site as normal but not show it to others yet.
Thanks in advance for any help.
Something like this should do the trick. It basically says: anything coming from an IP other than the one specified, load index.html.
Therefore when you load the page, you will see the rewrite take effect as it should, while everybody else will just see the index.html.
Make sure you use your public facing IP in the rule below.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1\.1\.1\.1 #your ip goes here, escape the dots!
RewriteRule .* index.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I do have a suggestion though. Try and setup a local development environment. You can change things as much as you want without afffecting your users...
This may be useful:
http://www.askapache.com/htaccess/htaccess-for-webmasters.html
I recently needed to do the same. I came to the conclusion that using a .htaccess file is very annoying especially if you have a dynamic IP. Basically what I've been doing is using an auth library I've been writing to restrict access to functions I don't want people to see and then check their role ID and redirect them to a static site.
So my role ID is 5 and a guest is 0. So all visitors would be 0 and so you could go restrict(array('5'), 'http://www.mysite.com/static.html') in your controller and perhaps use a redirect.
My library might be of use to you: https://github.com/Vheissu/WolfAuth-for-Codeigniter-2.0- there are of course many ways of doing things like this. This is how I've combated it. Hopefully this helps you.

Rewrite url issue

I'm hoping someone can help me. For my website I have a corresponding mobile site that has the same content as my full site but display it for mobile devices. Basically I want to send all requests from the full site to the mobile site unless the url variable sms exists
So in my htaccess file for my full site I have this:
RewriteCond %{QUERY_STRING} !sms=1 [NC]
RewriteRule ^(.*) http://mobile.mysite.co.uk/$1 [QSA,NC]
But when I got to www.mysite.co.uk/news/index.cfm&sms I get the following ColdFusion error for the full site:
File not found: /news/index.cfm
With debugging turned on I've noticed that the CGI variable PATH_TRANSLATED has been changed from
C:\webistes\mysite\news\index.cfm
To
C:\JRun4\bin\http:\mobile.mysite.co.uk\news\index.cfm
I'm at a loss to undestand what's going on? Any help or insight would be greatly appreciated.
Additionally I'm running a multi server install of ColdFusion 8 and using Apache configured for ColdFusion.
It seems like you want to make an external redirection, but your RewriteRule currently only rewrites the URL internally. Try adding the R and L flags to your rule to see if that makes a difference:
# Stop and redirect immediately to the mobile site
RewriteCond %{QUERY_STRING} !sms=1 [NC]
RewriteCond %{HTTP_HOST} !^mobile
RewriteRule ^(.*) http://mobile.mysite.co.uk/$1 [QSA,NC,R,L]
I also added in a RewriteCond to make sure that it doesn't redirect you if you're already on the mobile site, in the event that both of your sites point to the same place (you can remove it if they don't; just wanted to save you the headache in the event that they did).

Redirect dynamic subdomain to same subdomain with subpage. How?

I'm a little stuck in here. I need to get some help with this subdomain-situation.
I need to redirect http://dynamicsubdomain.example.com/ to
http://dynamicsubdomain.example.com/account/welcome.html.
How do I do this? I tried several things but all without result. The main problem is that I can't fetch the entered dynamic subdomain from the %{HTTP_POST} string from mod_rewrite.
Another issue would be that it's creating and endless loop. So it only needs to redirect on these conditions, not when there's a URL like http://dynamicsubdomain.example.com/test/page.html. Because else it will create and endless loop. It's just for the starting page from the website.
I hope y'all can help me out, it's one of the last but important things from my project.
Thanks in advance!
There are several options on the URL redirection wiki page. For example, how about dropping an index.php in the root that redirects to the destination?
header("Location: http://dynamicsubdomain.example.com/account/welcome.html");
Why does the domain matter so much if you are staying on the same domain, and just redirecting to a different path?
The UseCanonical setting in Apache may have an effect on this, but it is defaulted to on, which preserves the host and port specified in the request.
RewriteRule ^/$ /account/welcome.html [R,L]
Hey guys, thanks for your support/help but I found the solution myself. Quicker than I thought :)
This is what does the trick, I hope I can help someone with this:
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{HTTP_HOST} ^([A-Za-z0-9]+).example\.com [NC]
RewriteRule ^ http://%1.example.com/account/welcome.html [L]
#gahooa: I need to do this because my mainpage example.com is just a sort of landing-page with no special things. The extra part of the URL "account/welcome.html" is for showing a page related to the "subdomains"-account (gahooa.example.com for example will show your profile page). In my app I catch up the subdomain by a preg_match so it knows witch account it has to load. I hope I'm clear :) But thanks anyway!
This is my first time i'm using Stackoverflow but it actually works great! Quick replies from experts, great work! I definitely will come back :)
If you really want to use HTTP_HOST:
RewriteRule ^$ http://%{HTTP_HOST}/account/welcome.html [L,R]
But like gahooa already said you don’t specify an absolute URL if you stay with the same host. The URL path will suffice:
RewriteRule ^$ /account/welcome.html [L,R]

Resources