yii, mamp, htaccess rewrite urls - .htaccess

I am trying to rewrite yii urls but with no luck. I have spent hours going through sites and come up with the same answer each time which doesn't work for me:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#user-friendly-urls
I would like to resolve the urls to basic paths e.g
/index.php/site/index
to
/
/index.php/ads and /index.php/ads/
to
/ads
/ads/details?ad=9
to
/ads/9
The problem seems to be that the .htaccess has no effect.
I am using:
mamp pro
on a mac with lion
and the web directory is different to the webserver root.
I have set AllowOveride through the console.
The .htaccess is in the same folder as the main index.php but doesn't register even if I create an error.
I have had no problem with other non-yii web directories using an htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php
//main.php
'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>',
),
),

I have a project where my Yii application is in a subfolder as well.
This is my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Then in main.php, you'll need to add these lines to the 'rules' array:
'' => 'site/index', // This is for the home page
Is ads the name of your controller? In other words, do you have a file named AdsController.php in your controllers folder? If so, then it will work by default.
Just curious why you're hitting the index.php directly? You should be able to setup your localhost to point directly to your Yii app folder. For example, on my machine I created http:// mytestapp/ and it points to the app's folder in htdocs.
Since you're on a Mac, you need to edit your etc/hosts file as well...
I added a line like this:
127.0.0.1 mytestapp
Hope that helps...

Related

URL rewrite in subfolder css not working

How are you today,
I have a problem in url rewriting..
I Upload my website on pesco sub-directory, Like www.example.com/pesco and my css path like this /css/style.css but when site run css must be like www.example.com/pesco/css/style.css but my css showing like this www.example.com/css/style.css url rewrite missed the pesco sub-directory
Please any one give me the solution
My Code is below you can see.
ErrorDocument 404 /notfound.php
RewriteEngine on
RewriteBase /pesco/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html index.php
RewriteRule ^aboutus.html aboutus.php
RewriteRule ^([a-zA-Z0-9_-]+).html$ products.php?catid=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ detail.php?category=$1&product=$2
I think your error is just a reference problem. You can try ./css/style.css as this means it looks to find css as a directory inside the same directory as the file requesting it, whereas /css/style.css is essentially looking in the root directory

How to setup virtual directories with .htaccess

I'm trying to setup virtual directories with .htaccess.
I want to use this directories as language indicators. E.g. example.com/de-de, example-com/de-en and so on. But in fact, the directories doesn't exist on the server and the index.html file at the root of the directory should be loaded. I wrote some rules, but they dont work properly:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
URLs like example.com/de-de works, but example.com/de-de/ doesn't. When I open example.com/de-de/ it tries to load resources from the /de-de/ subdirectory, e.g. de-de/js/jquery.js instead of /js/jquery.js. Whats wrong with my rules? And is there a way to force a trailing slash, so users will be redirected from example.com/de-de to example.com/de-de/?

htaccess - alias a old path to a new path

I'm not familiar with servers and htaccess commands. I think I have a simple question.
I put a index.php file on the root of localhost. That file will generate the following the following pages on this links:
http://www.website.com/index.php?page=home
http://www.website.com/index.php?page=services
http://www.website.com/index.php?page=contact
Old path adress: http://www.website.com/index.php?page=home
New path adress: http://www.website.com/home
When user access the new path adress, I want the server to show what old path contains.
I want to use alias, no redirect.
Thank you for your time.
Make sure the links in your content all look like: http://www.website.com/home then add these rules to your htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?page=$1 [L,QSA]

Yii and .htaccess file on production server

I am new to yii and have some problem in configuring .htaccess file on production server.
On localhost :
Location of Application : /www/connect_donors/
Default URL that yii provides is,
http://localhost/connect_donors/index.php?r=controllerId/functionName
We used the urlManager in /connect_donors/protected/config/main.php to configure the SEO friendly url's..
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
Now the URL that was working was
http://localhost/connect_donors/index.php/controllerId/functionName
Then I used the .htaccess file to remove index.php from the above URL.
Location of .htaccess is : /connect_donors/.htaccess
Following is .htaccess file,
RewriteEngine On
RewriteBase /connect_donors
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
URL Chenged to :
http://localhost/connect_donors/controllerId/functionName
Everything working fine and awesome.
But yesterday I uploaded the application on production server.
On Production Server
Everything remained same only I had to change the .htaccess file.
The .htaccess file on server is,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Now the following url:
http://donorsconnect.com/
loads the home page of server properly.
But,
http://donorsconnect.com/profile
redirects again to home page.
NOTE : There is no session set on the "profile" controller.
class ProfileController extends CController
{
public function actionIndex()
{
$this->render('index');
}
}
I tried lot of things, changing the .htaccess file to different codes. but none helped me.
Any help is appreciable.
Solution
I finally got the solution and the mistake I had done.
My Components had a request array containing baseUrl.
'components'=>array(
...
'request' => array(
'baseUrl' => 'http://donorsconnect.com',
...),
Due to this it was not loading. I did not find the real reason for that.
But after removing that 'request' array, its loading fine.
Check link,
http://donorsconnect.com/profile
You should not hide index.php like this, read carefully : http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
You should set showScriptName to false in your main config :
'urlManager'=>array(
.....
'showScriptName'=>false,
.....
),
And your .htaccess should look like this :
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

Codeigniter site replicated in subdirectory not displaying individual pages

We have just copied a Codeigniter website (from our root domain) to a subdirectory on the same root domain (with all config.php changes made and a new database etc.). However, page links don't display the individual pages yet the browser simply reloads the index.php page content (as if it's loading a new page) and changes the URL in the browser window to the correct page.
This is really odd and I've spent hours pouring over it, so I'm hoping someone here may be able to give me a starting search point.
For your information, the .htaccess in the subdirectory is:
RewriteEngine on
RewriteRule ^test.php$ mod_rewrite.php
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
if theres a new codeigniter install in a subdirectory now that used to be on the root, you need to include the name of the sub directory in front of the index.php parts of your htaccess
something like this(the change is on the last line):
RewriteEngine on
RewriteRule ^$ /dev/index.php [L]
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico|zip\.php|resources)
RewriteRule ^(.*)$ /dev/index.php?/$1 [L]
dev should be changed to the name of your subdirectory. also, if neither or these works and you have a htaccess on your root, it could be effecting this subdirectory.
This is now sorted after a lot of different attempts!
File config.php in root -> application -> config (line 46) was originally:
$config['uri_protocol'] = ($_SERVER['SERVER_ADDR'] == '127.0.0.1') ? 'AUTO' : 'ORIG_PATH_INFO';
It should be:
$config['uri_protocol'] = ($_SERVER['SERVER_ADDR'] == '127.0.0.1') ? 'AUTO' : 'PATH_INFO';
All's now functioning correctly.

Resources