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!
Related
Imagine yourself a simple symfony install:
symfony new testsite
Now imagine this is inside the webroot of an apache server. Of course this won't work since the symfony webroot is in the /web/ folder.
So you make a simple rewriterule and drop it in a htaccess in the symfony folder:
RewriteEngine On
RewriteRule ^(.*)$ web/$1 [QSA,L]
And this works. Hooray! So you try out the "Create your first page in symfony" guide and lo and behold your controller's 404ing!
So it seems like symfony is cutting off 3 characters from the start of the path. A bit of debugging shows it's getting the base url from the $_SERVER['SCRIPT_NAME'] which is the cause of the problem, since this doesn't change when you rewrite the url.
Is there a proper way to fix this or do I just have to redefine SCRIPT_NAME manually in my app.php?
if you comments are correct, then the URL you should be using is:
symfony-test.local/web/app_dev.php/lucky/number
You should specify the DocumentRoot as the web/ folder! That is normally what is done. Read this for more details:
http://symfony.com/doc/current/setup/web_server_configuration.html
I have a magento website, that is currently operating. Within a subfolder of this site, I have placed a 3rd party application that also has its own HTACCESS file to handle routing for its application.
When I access the folder http://example.com/somefolder the screen I expect shows up, but when I navigate to http://example.com/somefolder/newroute, I instead land on a magento 404 screen.
I have traced this to the magento htaccess file, in all cases, unlesss the path physically exists the rewriterule will always send the request to the index.php - this explains why Im getting there.
To fix this issue, I wrote a little rewriterule which I placed in the htaccess file of the magento store. The goal was to add an exception to any request that came through and contained any reference to my subfolder. The thought is now it should hit the path its supposed, then hit the htaccess file, and then route me to where IM supposed to be in this other application. Unfortunately it doesnt seem to work, after adding the rule I end up the same place - magento.
Here is what I've written:
RewriteRule ^(.*somefolder.*)$ $1 [L]
Im not sure what could be going wrong, as I think the approach seems pretty straight forward. Any ideas on how to remedy this situation?
Thanks.
Here is Your Simple Answer.Also Used By me on my site.
RewriteCond %{REQUEST_URI} !^/(yourfoldernameHERE)$
I am confused about how a php framework works with a web server like apache. Now if there wasn't a framework and if it was using classic php, then I understand how it works(if a php file is requested, the php parser executes it and returns the htm to the server).
When a framework like cakePHP is used, I have noticed that no matter which url the client requests, the index.php in the root folder gets executed first. How is this possible? if we were using pure php then, only the file we requested will get executed. So how does cakePHP make each and every request to go through the /index.php file?
CakePHP, and many other websites, leverage mod_rewrite which is an Apache module that "Provides a rule-based rewriting engine to rewrite requested URLs on the fly".
To do so, the web framework will use a .htaccess file with specific rules defined. These rules, in the case you're asking about, point any URL matching a specific pattern to to a real index.php file (this file can be named anything really, but index.php is a good "default" name).
Per CakePHP's URL Rewriting outline, a base rule is defined as:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/to/cake/app
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
What the above does is:
Turns on the rewrite module
Sets two conditions for rewriting the current URL
a. Only if the requested file is not a real directory (!-d)
b. Only if the requested file is not a real file (!-f)
Sends the full requested URL, (.*) to index.php
Sometimes, you'll see the RewriteRule line as:
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This will set the requested url into the $_GET['url'] parameter instead of forcing the application to process the $_SERVER['REQUEST_URI']. Other times, there will be a lot of rewrite rules, some complicated, some basic.
There are a lot of good resources online, and in books, regarding mod_rewrite. Check out Introduction to URL Rewriting for one (it's one I have bookmarked, has a lot of good basic rules / formats).
That's a pretty simple one (https://github.com/gilbitron/PIP).
Read the files in the System folder.
Basically it goes like this:
All requests are sent to the index file (with mod_rewrite, or like a plain get parameter).
The index file includes configuration files and definitions of constants.
Then it loads the main class for the framerwork.
The main class does some configurations, initiates a database connection and so on.
Depending on how it is implemented, there might be a router but it's possible there isn't.If there is one the main class checks what controller should be loaded against the request in the router.
Then the main class loads the controller that should be loaded with that particular request.
PIP is not that good and the main class is actually not a class but you can get the point in it.
You can also read about the following things:
Object oriented programming: http://php.net/manual/en/language.oop5.php
Scopes: http://php.net/manual/en/language.variables.scope.php
Autoloading classes: http://php.net/manual/en/language.oop5.autoload.php
You mentioned
"I have noticed that no matter which url the client requests, the index.php in the root folder gets executed first."
This is what front controllers do. You can have a front controller with a framework or if coding from scratch using the .htaccess file. Front controllers enable rendering of pages using PHP functions/methods, such as $app->get($uri, $callback); in Lumen.
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.
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]