I have been searching this but could not find the answer. I might have missed something here. Basically I have two applications under one CI installation. Each of the app will have their own subdomain. So for example http://foo.example.com/ which will redirect to http://example.com/foo and http://bar.example.com/ which will redirect to http://example.com/bar. The http://example.com/ itself will be some kind of a landing page.
The redirect part is working fine. But it keeps changing the URL from http://foo.example.com/ to http://example.com/foo, which I want to prevent. I have read a solution to use a [P] instead of [L] flag, but that will require me to enable proxy.
Is there any other option for me to do this other than enabling proxy?
Currently my htaccess file is as below:
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# The line for subdomain
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,NC,QSA]
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
Thank you very much for the response.
Related
I recently upgraded my website, changed the underlying system, and pushed the old content onto a subdomain.
I want the google indexed URLs to the old content to still work, so I added a rewrite rule to the main htaccess that I thought would redirect using looking for the old 2015 posts, but it doesn't appear to be working.
I have added the rule to the top of CraftCMS's default htaccess, which is at the root of my domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/2015(/.*)?$ http://old.keithcod.es/2015$1 [R=301,L]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
I clarify, I'm trying to redirect all traffic from /2015/* to old.keithcod.es/2015/*
I have a CI installation in my root domain. SSL certificate is installed and working properly. When I redirect http:// to https://www it redirects to https://www.www (an extra www), that too on some computers and some browsers as users have reported. However, when I remove 'www' from redirection, its all fine. Seems like www is looping. So far, I've digged my code hundred times, and see no sign of redirection from code (I mean addition of extra www). I'm doing it with htaceess. Any help will be highly appreciated. This is my htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Force SSL
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R,L]
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
I'm running it on apache with Centos VPS.
Thank you so much!
The HTTP_HOST is the "target host" part of the request, like: www.mydomain.com.
The REQUEST_URI is generally the path which was given in order to access a certain page; for instance, ‘/folder/index.html’.
In your RewriteRule you say to put 'www.' in front of the requested domainname.
You don't want that, when someone asks for http://www.yourdomain.com.
Without the www. in your RewriteRule someone who requests for http://yourdomain.com gets redirected to https://yourdomain.com
When you want to redirect to https and www you need to add conditions. Look into Apache docs on Canonical host and on questions/4083221/how-to-redirect-all-http-requests-to-https for this solution:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTPS_HOST} !^www.yourdomain.com$ [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [L,R=301]
I know there are lots of question on htaccess, yet I tried the different code I could find on Google and StackOverFlow, none worked.
I have the following in my root :
index.php
.htaccess (the one I am trying to write)
controllers
--index.php
--mycontroller.php
models
--mymodel.php
view
-index.php
--myview.php
(I am working on localhost with MAMP&Firefox)
What I have is this link
localhost:8888/MySite/controllers/mycontroller.php
What I want is
localhost:8888/MySite/mycontroller
And when I manually enter the url, I would like it to be redirected to the right controller in my MVC code
I tried this :
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)$ /controllers/$1 [L]
It doesn't redirect when I go to blabla/controllers/mycontroller.php and doesn't understand what I am asking when I manual go to blabla/mycontroller.
If your base is in /MySite/ then it needs to reflect the RewriteBase:
RewriteEngine On
RewriteBase /MySite/
# match against the php filename
RewriteCond %{REQUEST_URI} ^/Mysite/(.*)$
# check to see if the request, routed through controllers actually points to an existing file
RewriteCond %{DOCUMENT_ROOT}/MySite/controllers/%1.php -f
# rewrite
RewriteRule ^(.*)$ controllers/$1.php [L]
This should take a request for the URI: /MySite/foo and rewrite it to /MySite/controllers/foo.php if there's a foo.php file in the controllers directory.
Did this to make it work :
RewriteEngine On
RewriteBase /MySite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ controllers/$0.php [L]
I have the following rewrite rules to redirect everything to index.php
RewriteEngine On
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]
Now I need to modify it to support the following
When user tries to access domain.tld redirect him to www.domain.tld
When user tries to access domain.tld/key/... redirect him (permanent redirect) to key.domain.tld/...
Of course I still need to redirect all traffic to the index.php except for when user tries to access css, javascript and images where I need to serve them directly
domain.tld/css/...
subdomain.domain.tld/css/... apache should directly serve the url (css files)
Similarly I need to add js and images folders
I am awful with Apache ReWrite module so PLEASE help me out here :)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f #if a file, css, js, jpg whatever file
RewriteCond %{REQUEST_FILENAME} !-d #if thats a directory
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^$ /someprofile.php?username=%2 [QSA,L]
</IfModule>
I have a CodeIgniter project running on CentOS, with an EV cert successfully installed.
I don't want SSL used when the /rss directory is accessed. I had this working, but I broke it somehow during an upgrade.
Here is my .htaccess file, which I think is correct:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# redirect to SSL connection
RewriteCond %{REQUEST_URI} !^/rss/
RewriteCond %{SERVER_PORT} 80
# R=301 sends HTTP Moved Permanently L=Last rule for this request
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
Going to the site, the url is correctly changed to https://www.site.com
But going to www.site.com/rss also gets changed to https.
I don't want it to use ssl for that directory.
I think the .htaccess file is correct, so maybe it is a CodeIgniter thing...
IF anyone has an idea, I would be very grateful.
What might be happening is that a mod_dir is not intervening in the order that it did before (mod_dir by default will redirect, if you access a directory but don't include a trailing slash, to the same URL with the trailing slash). If you try to access http://www.site.com/rss RewriteCond %{REQUEST_URI} !^/rss/ matches (since the request uri starts with "/rss" != "/rss/") and it gets rewritten to https://www.site.com/rss. Then mod_dir takes over and redirects you to "https://www.site.com/rss/". It's possible that before the upgrade, mod_dir applied the redirect first so you get redirected to "http://www.site.com/rss/" then the first condition (!^/rss/) fails, thus you don't get rewritten to https://
Try changing the first rewrite condition to:
RewriteCond %{REQUEST_URI} !^/rss/?$
So URI's like "/rss" and "/rss/" will fail the match and not get redirected to https://