ExpressionEngine: mod_rewrite directory to subdomain, while removing /index.php/ - mod-rewrite

In ExpressionEngine, what’s the best way to mod_rewrite a directory to a subdomain, while keeping index.php out of the picture?
For example:
http://www.domain.com/index.php/group/template -> group.domain.com/template
I’ve seen variations that take ANY group and rewrite them to subdomains, but I only need one.
I’ve been tasked with porting over a subsite from a different server (that was also running EE). Normally, I’d just redirect group.domain.com to domain.com/group (index.php removal was already working), but that’s been deemed an unacceptable solution. And of course, this is time-sensitive.
I’ve been diving into Google and the EE docs/wiki for going on twelve hours and I’m starting to go cross-eyed. Can anyone give me a hand?
Thanks in advance.

Here's how I would craft your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(group|group/.*)$
RewriteRule ^(.*)$ http://group.domain.com/template/$1 [L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
This example uses the "File and Directory Check" Method of removing index.php from the URL and uses a RewriteCond Directive to instruct Apache to handle the requests for the "group" directory and all its sub-directories differently.
Any links to domain.com/group/template will be redirected to group.domain.com/template/.
If you care about letting crawlers know your content has moved and want to make the transition as seamless as possible, you can add a 301 Redirect to your RewriteRule:
RewriteCond %{REQUEST_URI} ^/(group|group/.*)$
RewriteRule ^(.*)$ http://group.domain.com/template/$1 [R=301,L]
This will ensure that users and search engines are directed to the correct page.

Related

CodeIgniter and specific rewrite rule

On my CodeIgniter site, I would like to add a specific rewrite rule, so that this url
http://www.exemple.com/cache.manifest
would rewrite to
http://www.exemple.com/controller/manifest
(because Safari 7 seems to only accept .manifest files for ApplicationCache)
So I try to add this line to my htaccess
RewriteRule ^cache.manifest$ controller/manifest
I added it before the other rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^cache.manifest$ controller/manifest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
But it returns a 404. If I change the line to
RewriteRule ^cache.manifest$ test.html
it works. So the first part on my rule is correct.
If I try to access directly to www.example.com/controller/manifest, it works to, so my url is correct.
I tried also
RewriteRule ^cache.manifest$ index.php/controller/manifest [L]
But it doesn't work either…
Any clue ?
Thanks a lot
I tried some tests on my local server and I think the following might work:
RewriteRule ^cache.manifest$ /index.php/controller/manifest [R,L]
I am not entirely sure if you need the leading "/" or "/index.php", so you
may need to experiment.
You need the [R] flag to force a redirect. In this situation, you want Apache
to look for the string cache.manifest in the URL, and then go to the CI page
controller/manifest.
It appears that you need to explicitly set the redirect.
Please let me know if this works. Good luck!

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

Using mod_rewrite to redirect old urls with Codeigniter

I'm redeveloping a website using the codeigniter framework.
When we go live, we want to ensure a few of the old URLs will be redirected to the appropriate pages on the new site.
So I put what I thought would be the correct rules into the existing htaccess file, above the other rules that CodeIgniter applies.
However, they are not taking affect. Can anyone suggest what I'm missing here?
# pickup links pointing to the old site structure
RewriteRule ^(faq|contact)\.php$ /info/ [R=301]
RewriteRule ^registration\.php$ /register/ [R=301]
RewriteRule ^update_details\.php$ /change/ [R=301]
# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^_system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# This snippet prevents user access to the application folder
# Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^myapp.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# This snippet re-routes everything through index.php, unless
# it's being sent to resources
RewriteCond $1 !^(index\.php|resources)
RewriteRule ^(.*)$ index.php/$1
Try adding a [L]ast flag to your R=301 flag => [L,R=301] that makes sure no other rules are applied, and, just to be sure, try to redirect to a complete URL and, to be even more sure you haven't deleted anything, add RewriteEngine On to the top and set the RewriteBase.
Make your first rows look like
RewriteEngine On
RewriteBase /
RewriteRule ^(faq|contact)\.php$ http://www.YOURDOMAIN.XYZ/info/ [L,R=301]
and check if the URL in your browser changes when you call for instance the faq page.

404 is given when trying to access existing controller in CodeIgniter

I am working on a fairly new install of CodeIgniter 2.0.
I have a htaccess-file containg this code
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Going to the URL http://www.estiem.no/ESTIEM/CI/ works,
but http://www.estiem.no/ESTIEM/CI/site/index does not. It gives a 404. The controller Site exists, and contains the method 'index'
Any ideas what might be wrong?
You need to remove / from index.php
RewriteRule ^(.*)$ index.php/$1 [L]
It is a htaccess problem, back when everyone passed that code around we found that a lot of hosts didn't like it and you could hack it with a ? or this or that until it ended up looking like a jigsaw puzzle (the condition that is) but using a brilliant htaccess (not sure who the first was but like 500 have taken credit) you will never have to change it again (well not never but it will work 99% of the time un edited):
# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Trust me I have had the same issues and spend hours on google when i should have come here or even the CI wiki (since that is the first place I seen it), you would not believe how many issues are solved in CI with just that bit of htaccess. they really should add it to the repo.
I just noticed that since you have your CI install in a subfolder, you need to account for that in the .htaccess rewrite:
/ESTIEM/CI/
So your .htaccess will look like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ESTIEM/CI/index.php/$1 [L]
That should fix it (sorry late edit, due to #BrianOrtiz 's comment)
Try to access the controller with: localhost/site/index.php/controller
If that does not work maybe because the CI is case sensitive
localhost/site/controller == c:...\site\controller.php
localhost/SITE/controller == c:...\SITE\controller.php
else
<?php
controller Mycontroller extends Controller {
}
?>
will be named mycontroller.php in the controllers folder
If all else fails replace your htaccess by this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

mod rewrite howto

I use the below mod rewrite code for urls like: www.site.com/play/543
RewriteEngine On
RewriteRule ^play/([^/]*)$ /index.php?play=$1 [L]
How can I expand on this so I can have a couple urls like www.site.com/contact and www.site.com/about
RewriteRule ^(play|contact|about)/([^/]*)$ /index.php?$1=$2 [L]
Might do the trick. Now requests to /play/foo points to /index.php?play=foo and /contact/bar points to /index.php?contact=bar and so on.
Edit, from comment "about and contact will never be set though."
Just use two rewrites then;
RewriteRule ^play/([^/]*)$ /index.php?play=$1 [L]
RewriteRule ^(contact|about)/?$ /index.php?a_variable_for_your_page=$1 [L]
The common way used by most of the PHP frameworks is just passing whatever the user requests except existing files (generally assets *.png, *.js *.css) and/or public directories to a PHP script and then interpreting the route on the PHP side.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d #if not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f #if not an existing file
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] #pass query string to index.php as $_GET['url']
</IfModule>
On the PHP side it is very important to avoid things like this
$page = getPageFromUrl($_GET['url']);
include($page);
So be very careful when taking the user input and sanitize/filter to avoid the remote user to access non-public files on your web host.

Resources