I'm having a scenario where I had to deploy multiple directories for different languages. Development has initially centered on one of those languages, and just one part of the whole site is a small CI app.
I'm trying to avoid copying the whole app for the several other languages, and redirecting to it with an .htacces. The latter is working fine, but CI returns a 404 error when accessed from a URL different to the real one.
My best guess is that certain configuration files must exist with unique properties that configure the additional root URLs, but I don't know where to start (and Google didn't come up with a similar scenario).
File Structure:
public_html/
lang1/
app/
(the actual CI app)
other static stuff...
lang2/
app/
.htaccess (redirecting to /lang1/app/)
other static stuff...
lang3/
...
Additional info:
The $config['base_url'] is set to http://.../lang1/app/.
The .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ /lang1/app/$1 [L]
I was able to accomplish this by my own. For those who might find this question in the future, these are the steps to access your application from a different directory. Considering the scenario introduced in the question above, the following example will work for the deployment of of a ghost version of the app in the lang2 directory:
1.- Copy only the index.php file from the root of your CI installation (from lang1/app/index.php) to the lang2/app/ directory.
2.- Edit the following lines:
$system_path = '../../lang1/app/system';
$application_folder = '../../lang1/app/application';
//Even if the documentation suggests the necessity of a _full path_,
//this works perfectly well for 2.1.0
3.- Add any configuration you may want to have set explicitly for said subdirectory, these will be set to $this->config, replacing the values set in the config file in your base application:
$assign_to_config['lang_version'] = 'lang2';
4.- You have to set a proper base_url. In this case, we could reuse the lang_version config we just included. This way, we can forget about this line in the next languages we need to create.
$assign_to_config['base_url'] =
'http://www.example.com/' . $assign_to_config['lang_version'] . '/app/';
5.- Create an .htaccess file inside lang2/app to make sure that any static assets (js, css, images) accessed by the HTML are get from the actual assets folder inside the original app directory, like in lang1/app/assets:
RewriteEngine on
RewriteRule ^assets/(.*)$ /lang1/app/assets/$1 [L]
6.- Add to this .htaccess your usual rule to keep your URLs friendly, but this time directing all traffic to the ghost copy of your app:
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ /lang2/app/index.php/$1 [L]
7.- Grab a beer or any other beverage you drink to celebrate success, always respecting your local laws. Profit. Your controllers will have to read the config('lang_version') value to present the content in the proper language to the user. You may use the included language helper for that, or any other solution you prefer.
Why would you do this? Theres a thing called i18n.
http://codeigniter.com/wiki/CodeIgniter_2.1_internationalization_i18n/
Why not use this? Didnt copying the app folders seem a bit tedious to you? Theres a language helper for a reason.
I hope this helps.
Related
I want to map URL in my localhost XAMPP into custom files.
For example:
localhost/index.php --> d:\xampp\htdocs\index.php (default)
localhost/normal/data.php --> d:\xampp\htdocs\normal\data.php (default)
localhost/view/userinfo.php --> d:\xampp\htdocs\view.php?p=userinfo (custom)
localhost/view/welcome.php --> d:\xampp\htdocs\view.php?p=welcome (custom)
So, basically, all URL that goes into inside view path will be mapped to view.php files with the filename.php (minus the .php) as its query parameter. There's actually no physical folder view, and no physical files userinfo.php and welcome.php inside the folder.
The reason that I need to do this is that so I can pass all the pages that viewing data into an "application frame" that will wrap the page with header, menu, and footer, and I don't need to give header, menu, and footer call in each page. I might have the actual files userinfo.php that I can $include_once, or I might not (I can just generate it from within the view.php), but hey, that's one of the power of this kind of framework, right? And, if someday I need to change this structure, I can change it from just within one file (view.php), not all.
Can I do this in PHP and XAMPP? How? I've noticed that some website seems to used this practice (URL which have no actual files or even path at all), but when I try to read tutorial for it, I got confused.
URL mapping in PHP?
The accepted answer listed 3 links to learn about URL rewriting. Mostly they're written for Apache in Linux, and mostly they pull all the possible scenario and configuration that I got confused which one I really need with all those long documents and technical jargon. I need just the practical step of my specific problem, and then, I will be able to start from there to explore myself if I have more advanced needs. Please help.
if you do want to go down the mod rewrite route adding the following to an .htaccess file in the site root should do it. You will need to make sure mod rewrite is on for XAMPP and I can't help you there I'm afraid. As you can see it rewrites the url, not the windows filename - so it would work on any OS.
The ([a-z]*) means it will take any filename.php with lowercase letters and redirect to /view.php?p=$1 where the $1 will be replaced by filename.
the [L,R] (L means last rule so stop processing if any more are reached, and the R means redirect (it will change the url in the browser). Use P instead to reverse Proxy (the user will still see the url they requested but the server will serve the correct file) - This will require mod_proxy as well.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/([a-z]*).php$ /view.php?p=$1 [L,R]
</IfModule>
XAMPP uses apache so the rewrites would work the same in Windows as they do in Linux. You could place a .htaccess in the site root directory with some rewrite rules.
However, using PHP
in d:\xampp\htdocs\view\userinfo.php you could include the line
<?php
header('Location: http://localhost/view.php?p=userinfo');
?>
But this must be before any thing is echoed to the screen (even whitespace).
You can use the Apache module mod_rewrite to edit requests before they hit PHP. You want to put something like the following in a .htaccess file in your htdocs directory.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/(.*)\.php.*$ view.php?p=$1 [L,QSA]
QSA means Query String Append. This means that if there are any GET parameters set on the original request they will be appended to the end of the new request too.
Note that this assumes that Apache is configured with AllowOverride enabled and the mod_rewrite module loaded.
I want to extend the following .htaccess rewriteCond to also check if the requested file exists within a subfolder in web (and serve it, not redirect - but serve the file)
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
for example I have the following files in sub-directories within Web folder of symfony project
static/contact.php
static/wiki/countries.php
the request URLs look like this
localhost/symfony/web/contact.php (www.domain.com/contact.php)
localhost/symfony/web/wiki/countries.php (www.domain.com/wiki/countries.php)
i do not want to redirect, just want to serve the contents.
Let me elaborate a little further.
I am currently in the process of upgrading an old php project to symfony2 framework.
The old project has the following URL structure for static pages
www.domain.com/faq.php
www.domain.com/contact.php
www.domain.com/wiki/countries.php
Now in order for this to work as it is - I have to copy the static page and the wiki folder in the WEB folder of the new symfony2 project. Which is fine and the above given Rule / conditions would work and the above URLs would work.
But I do not want to copy the static pages into WEB as it will clutter the WEB folder, and i want to keep it clean - So I moved the static pages to a sub folder within WEB folder called static. Now the above URLs do not work, and throw a page not found error coz all the pages are within web/static folder. so with the current Rule / cond the URLs will have to be changed to
www.domain.com/static/faq.php
www.domain.com/static/contact.php
www.domain.com/static/wiki/countries.php
Is there a way where I can keep the previous URL structure and keep the static pages within a sub folder in web? one way which i can think of is that to have a cond and rule in .htaccesss file within WEB folder to also check for requesting files within static folder and if it is found, then serve them (not redirect)
thanks for you time and help, much appreciated.
I am restyiling my own php mvc framework.
I have every request handled by /index.php by default, which triggers the mvc process of routing, executing the request and returing a proper view. Each request is routed according to a single 'q' GET parameter, drupal style, like
/index.php?q=anApplication/aController/theAction/arg1/.../moreArguments
This works pretty good, and makes the clean url thing easy via mod_rewrite. Ok.
I have a directory tree like this:
/public
|----themeName
|--------|
|----page.tpl
|----content.tpl
|----etc.
/private
|----sourceDirectory
|----moreSources
What i dont want is files stored in private and public directories to be served directly like an HTTP request: i dont want something like
mySrv/public/themeName/page.tpl
to show a dead template, or any resource that is not an image, bypassing my core handler - index.php.
I think i could achieve something with a rewrite configuration like this
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]
but then the framework would only work on mod_rewrite-enabled sites, because this will rewrite all existing and non-existing resources.
What i am asking for is: is there another way to make EVERY request served by a resource (such an index.php) of my choice, existing or non-existing ones?
Thank you
Just store all your templates etc outside of the public_html folder.
This will allow PHP to have access, but the outside world cannot get to the file.
The easiest and more portable way would be to pull everything except your index.php out of the document root. PHP can still include files and do everything else.
I have not tried this, but if you put an index.php outside the old document tree
---/ app / new-index.php
|
/ public /
|
/ private / ...
|
index.php
and then add at the beginning of new-index.php
<?php
chdir('..');
require 'index.php';
?>
and finally reconfigure Apache so that the DocumentRoot actually becomes /app, then everything should work as before -- except that any URLs but '/' stop making sense for Apache, and all can be made to land on a suitable ErrorDocument 404.
Note: "everything should work", except HTTP redirections. You can read a non-interpreted PHP file from the FS, but you can no longer get its interpreted content from, say, Apache on localhost. Also, you ought to verify that any existing code does not make use of the DOCUMENT_ROOT global variable; if necessary you may overwrite it.
A site has an existing system (lets call it mysite)
and the client asks to put in magento.
My directory structure goes something like this:
ROOT
-index.php (this is the app's main controller)
-.htaccess
/blog (runs wordpress)
/assets (current system's media folder)
/magento (this is where all magento files go)
Problem is if I set up magento and specify in the installation that base URL is http://example.com, magento loads up mysite.
Leaves me no choice but to setup magento with base URL set to http://example.com/magento/ and it runs perfectly.
However the client wants me to feel hell and asks me to hide magento in the URL.
I’m not really versed in .htaccess and I know only simple rewrite codes so I tried forwarding any HTTP requests that start with /magento to the magento folder and came up with:
RewriteCond %{REQUEST_URI} !^/magento(.*)
RewriteRule (.*) /magento/$1 [L]
Just when I thought it was working, mysite links all became unaccessible and forwards to the magento system displaying it's 404 page.
So, uhm, can I ask for help how to construct the .htaccess to hide the /magento/ on the URLs without affecting the current system aka mysite?
Because you have existing applications off the webroot, you cannot get away with using nothing instead:
### webroot/.htaccess
RewriteRule ^whatiwanttouseinsteadofmagento/(.*)$ magento/$1 [L]
From how I see the problem you will not be able to hide magento completely and use your site as well in the same time.
If you want Magento in the root of the public folder you should just point the virtualHost to your magento installation but this will let your blog and your main controller out of the public view. This is more or less the same with what you did by redirecting all calls in the .htaccess to magento folder.
What I suggest is to change the magento name to something more anonymous like "shopping" or "cart", and remember that a folder rename is preferable to a .htaccess file in terms of security and performance.
Let's look at it:
RewriteCond %{REQUEST_URI} !^/magento(.*)
So we're saying the condition is anything that is not /magento(.*), so everything but that directory? This would redirect everything, including your blog, assets, and any other directories.
Without specifying each and every file that needs to be redirected to the magento directory, there really is no easy way of doing it. I suppose you could redirect any file that does not contain a "/" in it and ends with the extension .php to the magento directory. That way only files in the root web directory will redirect to magento, but if you used other directories inside the magento directory you'd still need to add separate rules for them.
this answer comes very late but I guess you wanted something like
RewriteCond %{REQUEST_URI} !^/(blog|assets|magento)(.*)$
RewriteRule ^(.*)$ /magento$1 [L]
My backend URLs look like this:
mysite.com/backend.php/blog
I'd like to change it to:
mysite.com/backend/blog
Technically this isn't limited to admin apps, as Symfony grants every application two front controller scripts. But I hate having the script name in URLs and as such I'd like to change it. Is this possible?
Thanks in advance.
Edit
#codecowboy - I did resolve this by creating a 'backend' directory in the web directory. I then copied over the .htaccess file symfony puts in web, and I moved the backend.php and backend_dev.php front controllers to /backend and renamed them index.php and index_dev.php. Then within each front controller I tell PHP to look one directory further up for the project config class. I've been doing this for a while now and it serves my needs perfectly. I actually wrapped this all up in a task so that setting up a new admin app is a 1 step processs.
You can add
#I'm no regular expression expert or mod_rewrite expert, this line probably has some bugs
RewriteRule ^backend(.*)$ backend.php [QSA,L]
to your .htaccess file right before
RewriteRule ^(.*)$ index.php [QSA,L]
and that solves 1/2 of your problem. Anything sent to yoursite.com/backend/xxx will be routed through backend.php. The other problem you get is with internal symfony routing. It will interpret yoursite.com/backend/xxx as a request for module "backend" and action "xxx". I'm sure it's not too hard to solve. Good Luck!