FearGannicus
FearGannicus

Reputation: 205

GoDaddy .htaccess for a subdomain

I have been developing a website, and my .htaccess for a subdomain doesn't seem to be working right.

The .htaccess file in my subdomain.

DirectoryIndex index.php
Options -MultiViews

RewriteEngine on

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+home\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ home.php?page=$1 [L,QSA]

When I goto my subdomain, I can get to me index.php, but when I goto my home.php or /home etc it doesn't seem to work, and redirects to my 404 page (which has been defined in my root directory.

My Root Directories .htaccess file:

Options -MultiViews

RewriteEngine on

ErrorDocument 404 http://example.ca/error?id=404
ErrorDocument 403 http://example.ca/error?id=403
ErrorDocument 500 http://example.ca/error?id=500

RewriteCond %{http_host} ^www\.james-emerson\.ca [NC]
RewriteRule ^(.*)$ http://james-emerson.ca/$1 [R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

RewriteRule ^home$ index.php

My Root looks something like:

/
 - content/
 - include/
 - sites/
   - external/
     - sbfg/ (subdomains subdirectory)
       - content/
       - include/
       - .htaccess
       - index.php
       - home.php
     - ...etc...

 - .htaccess
 - index.php
 - ...etc...

If you can help, I would be grateful. Thanks in advance.

Upvotes: 0

Views: 2889

Answers (3)

FearGannicus
FearGannicus

Reputation: 205

Figured out how to fix the problem on my own, with help from @anubhava.

The .htaccess file that @anubhava gave me:

DirectoryIndex index.php
Options -MultiViews

RewriteEngine on

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /sbfg//home\.php\?page=([^\s&]*)[&\s] [NC]
RewriteRule ^ /sbfg/%1? [R=302,L]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /sbfg/home.php?page=$1 [L,QSA]

Wasn't working correctly because my server was going to the url sites/external/sbfg/sbfg/home.php rather then sites/home/sbfg/home.php. It was until I removed the 404 document code from my roots .htaccess file, that I notices the error.

I than updated my code to:

DirectoryIndex index.php
Options -MultiViews

RewriteEngine on

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /home\.php\?page=([^\s&]*)[&\s] [NC]
RewriteRule ^ /%1? [R=302,L]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /home.php?page=$1 [L,QSA]

Then added some of the code to my root directories .htaccess file:

Options -MultiViews

ErrorDocument 404 http://james-emerson.ca/error?id=404
ErrorDocument 403 http://james-emerson.ca/error?id=403
ErrorDocument 500 http://james-emerson.ca/error?id=500

RewriteEngine on

RewriteCond %{http_host} ^www\.james-emerson\.ca [NC]
RewriteRule ^(.*)$ http://james-emerson.ca/$1 [R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
RewriteRule ^home$ index.php

# .htaccess file for the sbfg/home.php?page= code.
RewriteEngine on

RewriteCond %{THE_REQUEST} /home\.php\?page=([^\s&]*)[&\s] [NC]
RewriteRule ^ %1? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ home.php?page=$1 [L,QSA]

And now it works as it should. Thanks everyone for helping me, not sure why was having these problems before!

Upvotes: 1

anubhava
anubhava

Reputation: 785631

Try this in your sbfg/.htaccess:

DirectoryIndex index.php
Options -MultiViews

RewriteEngine on

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /sbfg//home\.php\?page=([^\s&]*)[&\s] [NC]
RewriteRule ^ /sbfg/%1? [R=302,L]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /sbfg/home.php?page=$1 [L,QSA]

Upvotes: 1

twicejr
twicejr

Reputation: 1330

Why not do it like this, and handle the elements that have to be excluded in php:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ home.php [QSA,L]

It's not the easiest, but this code should work and gives complete control over when to redirect. You can easily create a table that stores nice urls to obtain the controller / action / params, maintaining old urls easier, etc.

In php, this should work from every directory, poiting to the file where the request started (home.php)

setup_paths();
$current_url = get_current_url(false);
$explode_uri = explode('/', $current_url);
$page_like_you_have_now = array_shift($explode_uri);

function setup_paths()
{
    define('BASE_DIR', dirname($_SERVER['PHP_SELF']));
    $base_url = BASE_DIR;
    if(substr($base_url, -1, 1) !== DS)
    {
        $base_url .= DS;
    }
    define('BASE_URL', str_replace('\\', '/', $base_url));
}

function get_current_url($with_base = true)
{
    $current_uri_data = parse_url($_SERVER['REQUEST_URI']);
    $start = strlen(BASE_DIR);
    $uri = substr($current_uri_data['path'], $start);

   //Remove possible slashes on begin, or do other corrections
    while(substr($uri, 0, 1) == '/')
    {
        $uri = substr($uri, 1); //@should 301 to correct pages afterwards..
    }

    return ($with_base ? BASE_URL : '') . $uri;
}

Upvotes: 1

Related Questions