how to define rewriterules in cloud9 web-ide - mod-rewrite

I'm playing arround with cloud9 and I would like to define rewrite rules for my apache based web application in c9 web-ide.
I created a .htaccess file (not sure if is located correctly). I guess I need to configure httpd.conf file but can't find such.
how should it be done?
Thanks.

I had this issue. Turned out it was all down to typos in the .htaccess file. No further configuration was required.
I saved the .htaccess file in a folder I created called 'site' which I am using as the root for my web app (so the same folder which contains index.php), i.e. like this: myWorkspaceName/site/.htaccess
The full contents of my .htaccess file is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
You might copy and paste that to use as a test of the configuration (remembering to save and restart Apache of course).
In case you do need to configure something like httpd.conf it appears that you would want to take a look at the following files as a starting point:
/etc/apache2/apache2.conf
and
/etc/apache2/sites-available/001-cloud9.conf
Access to these files is via the command line, so you would type:
sudo nano /etc/apache2/sites-available/001-cloud9.conf
for example.
It may be worth mentioning that for my specific rewrites, the address I would use has to include my sub folder 'site', i.e,
https://myWorkspaceName-wkille.c9.io/site/anything
whereas
https://myWorkspaceName-wkille.c9.io/anything
throws a Not Found error.

Related

I want to keep the assets folder inside the application folder not outside

My question as in the title
I want to keep the assets folder which contain (css, js, images) inside the application folder not outside.
If I keep the assets folder inside the application folder, I can't access to the files that inside it.
.htaccess file code
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
The standard Application structure:
Application
app
assets
sys
.htaccess
index.php
The previous structure has no errors.
I want the structure to be like the following:
Application
app
assets
config
controllers
......
sys
.htaccess
index.php
How can I do that ?
Your problem is that the .htaccess file you're using is rewriting the URL of your static assets, breaking the links, so you need to change it.
If you keep your assets/ folder inside the root of the application and amend your .htaccess to something like this, that should solve the issue:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
The change is on the second line - essentially the condition is "If the route doesn't start with index.php, assets, or robots.txt, then rewrite it to include index.php at the start". The addition of assets should solve the issue.
EDIT: By default, CodeIgniter has an .htaccess file inside the application/ folder that denies access to anything inside it. If you really, really want to be able to serve your static files from that folder, you can amend that file. However, it would be a really bad idea. Your static assets should live in the web root, and trying to serve them from the application folder will be fraught with problems - if you allow access to the application/ directory from outside, it could cause some serious security issues.

Yii :: Issue Using Showscriptname = False And Url Manager

I am currently having an issue using the URL manager and/or the apache mod_rewrite or maybe something else entirely.
With showScriptName set to false, navigating to addresses such as domain.com/login, domain.com/logout, domain.com/site/login are all behaving the same way. It simply shows the main site/index, as if I were to navigate to domain.com/eeofjew9j8jfdedfmewf (jibberish).
Maybe it's an issue with my Yii settings? Here are those (sorry for the sloppiness):
'components'=>array(
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'login'=>'site/login',
'logout'=>'site/logout',
'register'=>'users/register'
),
,...
Here is how I have my .htaccess setup in the www root:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
I am using a VPS server, so I have root access to make any changes needed to apache. I've checked my phpinfo and mod_rewrite is running and enabled. In my apache config, I have for my www directory:
AllowOverride All
Options Indexes FollowSymLinks MultiViews
Order allow,deny
Allow from all
I've been scratching my head on this issue through 3 different hosts (godaddy, dreamhost, and lithiumhosting) so I'm assuming it's an issue on my end with this. Now that I have a VPS though, I'm hoping I can finally figure out my issue and solve it.
First verify that the server is parsing the .htaccess file. This is simply done by placing random text in the .htaccess file such as
CREATE AN ERROR
If this gives you a server error then the file is being parsed. You may have to move the .htaccess file up a directory to get this to work.
After this is working check to make sure the rewrite module in Apache is on. First check phpinfo and look for the running module. If not you may have to delete the comment (#) character in front of the module.
After the module shows up in phpinfo then see if you can do a basic rewrite to make sure that there are not problems with Apache.
RewriteEngine on
RewriteRule ^ http://google.com/? [L,R]
If that is not working try adding the
Options +SymLinksIfOwnerMatch
to the file
Once you have Apache doing it's part now it is up to yii. This code:
RewriteEngine On
RewriteBase /mybasedirectory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
adds the rewrite base option which may be necessary if you do not have the files in the document root such as htdocs
finally if that does not solve the problem then limit your rule to a simple rule such as
'contact'=>'site/contact',
and see if at least redirects to where you think it should. Of course you may have to check the basic rules of .htaccess again to make sure that overide is allowed and there isn't an errant .htaccess in a subdirectory. I hope that helps. I finally got mine working.
What happens if you delete these lines!
enter code here
# otherwise forward it to index.php
RewriteRule . index.php
if you add this code see if it works
'<action:(login|logout|register|contact)>' => 'site/<action>',

mod_rewrite with codeigniter

im using shared hosting. i build my web application with codeigniter and my new web directory will be as follows:
/home/projName/public_html/proj_v3/
under this dir, there are index.php, .htaccess. as shown above, i separated different project version in different directories (ie: proj_v1,proj_v2, proj_v3). the web applications allows photo upload and it's stored in /home/projName/public_html/proj_v3/application/uploads. proj_v2 currently has the latest uploaded photo since it's still in use. because i have different version of projects, i want to place directory uploads in a common directory such as /home/projName/public_html. so that for any new version of projects, i won't have to move the upload folder to the new project folder. this might cause downtime.
currently, the .htaccess file in /home/projName/public_html/proj_v2 is:
Options -indexes
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1/ [L]
i have another .htaccess in /home/projName/public_html/ is:
Options All -Indexes
RewriteEngine On
Rewriterule ^(.*)\.*$ proj_v3/index.php/$1/
RewriteOptions MaxRedirects=3
basically how should i go about editing the 2nd .htaccess file in /home/projName/public_html/ so that if the web directory is domain.com/uploads/image.png, it gets the image from /home/projName/public_html/uploads. currently, the 2nd .htaccess file tells domain.com to point /home/projName/public_html/proj_v3
RewriteRule \.(jpe?g|gif|png|wmv|bmp)$ /uploads/$1
Might work for you. You can add other formats as needed.
What you want to do is allow the paths to real files and folders to exist, while redirecting everything else to CodeIgniter, and you can achieve this using this in your second .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Definitely worth reading this article on the CodeIgniter website about mod rewrite
I actually gave a similar answer before: mod_rewrite in Users/<username>/Sites directory on OSX

Rewrite Rule(s) for domain aliases

Situation: I have a single (main) domain, which has several aliased domains, each of which are pointing at the same Plesk-based server (for instance, I have example.com as main, with something.net, anotherone.co.uk, and several others all as aliases of the main domain account). This means that whenever I enter the domain name into my address bar of any of the aliases, it goes directly to the account of the main domain (example.com).
Problem: Based on the domain name of the alias(es), I have an index.php that redirects each domain differently (for instance, requests to domain A redirects to a corporate site, domain b goes to a thanks site etc.) Which works great, but if a directory is added after the domain URL (i.e. somealias.com/something) then it gives a 404 not found error.
What I would really appreciate, if someone can help me out, is a (single if possible) rewrite ruleset that would essentially strip off ALL trailing directories and/or GET requests, and only leave the typed-in base URL, so then the php script sitting in the main domain document root can take over and deal with the request appropriately.
Strangely enough, I've not been able to find a (simple) solution for this anywhere. Is it a case of having to define a rule for each of the aliased domains individually?
Try the following,
#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomain.com$[OR]
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !=/
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule .* http://%{HTTP_HOST}/? [L]
This will take ALL requests except root folder / (e.g. http://example.com/) or index file (e.g. http://example.com/index.php) and redirect them to the root folder (e.g. http://example.com/some-url will be redirected to http://example.com/).
You may need to replace index.php by the file that is get executed when you hit the root folder (Apache will silently rewrite http://example.com/ to http://example.com/index.php (depending on your actual settings) as it needs to have a file to execute otherwise it may show an error).
Alternatively (possibly even better -- depends on your actual setup and requirements) you may use these rules -- this will redirect only non-existing URLs. So if you have an image meow.png on your site, these rules will allow you to access it (http://example.com/meow.png):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* http://%{HTTP_HOST}/? [L]
UPDATE:
If you going to place this into config file (httpd-vhost.conf or httpd.conf) then use these rules:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule .* http://%{HTTP_HOST}/? [L]
It seems to me that all the sites are hosted on the same server (probably using the same code base).
If your index.php is a front controller you can redirect everything to your index.php and decide in the first lines of index.php what front controller to load (like backend.php).
If you don't mind having to maintain a list of the aliases you can define a hash of [alias] => path-to-front-controller.
In the front controller of you main domain you check the alias name (using $_SERVER['SERVER_NAME'] for example) against the hash and load the appropriate file.
You will have to add and entry to the hash each time you add anew alias. If they are not generated dynamically maintaining this hash is not a lot of hassle.

How to reference the current directory from .htaccess using mod_rewrite?

I'd like to use mod_rewrite to make pretty URLs, but have a single version of the .htaccess file that can be used for any user on a server.
So far I have the standard pretty URL .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Ideally, I would like something similar to
RewriteRule ^(.*)$ %{URL of this file's directory}/index.php/$1 [L]
This documentation would lead me to believe that what I want is not necessary:
Note: Pattern matching in per-directory context
Never forget that Pattern is applied to a complete URL in per-server configuration files. However, in per-directory configuration files, the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done. This feature is essential for many sorts of rewriting - without this, you would always have to match the parent directory which is not always possible.
I still get the wrong result, though, whether or not I put a leading / in front of the index.php on the RewriteRule.
This,
http://server/test/stream/stream
turns into
http://server/index.php/stream
not
http://server/test/index.php/stream
when the .htaccess file is in /test/.
I was having a similar problem, but found that simply leaving off the root '/' resulted in my test web server (xampp on windows) serving a URL similar to:
http://localhost/C:/xampp/htdocs/sites/test/
Not perfect. My solution is to use a RewriteCond to extract the path from REQUEST_URI. Here's an example for removing the unwanted "index.php"s from the end of the URL:
RewriteCond %{REQUEST_URI} ^(.*)/index.php$
RewriteRule index.php %1/ [r=301,L]
Note: the percent sign in "%1/", rather than a dollar sign.
%1-%9 gets the patterns from the last matched RewriteCond, $1-$9 gets the patterns from the RewriteRule.
See:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond
Cheers :)
Turns out that it does matter whether I put a leading / in front of index.php or not. By leaving it off, the script works correctly. I had been testing with the R flag which was using physical directories on the redirect.

Resources