3

The content for my home page is pulled based on a query to the database using the slug 'home'. For example, if I enter example.com/home in the address bar, it queries the database based on seeing '/home' and pulls the correct content (using a variable '$path'). That part is working fine.

I also have a function that will redirect any unknown slugs direct to the home page. If I enter example.com/foo and there is no corresponding 'foo' record in the table, it will just serve index.php. Here is the function:

if(!isset($path['slug']) || $path['slug'] == '') { 

        header('Location:home'); 
}

I have looked at this answer and this answer which seem close, but aren't quite getting me what I need. I want 'example.com/home' to show as 'example.com' in address bar.

Also, here is my .htaccess:

# GZIP for speed
    <ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
    </ifmodule>


# Removes any canonicalization issues
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.example.com$
    RewriteRule ^(.*)$ "http://example.com/$1" [R=301,L]


#   RewriteRule ^/$ /home [L]


#   RewriteEngine on
#   RewriteBase /
#   RewriteRule ^& http://localhost/example.com/home [L,R=301] 
#   RewriteRule / http://localhost/example.com/home [L,R=301]



# Restrict access to a variety of private file types
    <FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
     Order Allow,Deny
     Deny from all
    </FilesMatch>

# Disable directory browsing
    Options All -Indexes

# Prevent folder listing
    IndexIgnore *

# Let index.php handle all requests
    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^ index.php [L]
    </IfModule>

# Dump referrer spam
    <IfModule mod_rewrite.c>
        RewriteEngine On
        Options +FollowSymLinks
        RewriteCond %{HTTP_REFERER} (?:o-o-6-o-o|bestwebsitesawards|s.click.aliexpress|simple-share-buttons|see-your-website-here|forum.topic55198628.darodar|hulfingtonpost|ilovevitaly|priceg|blackhatworth|semalt.semalt|kambasoft|buttons-for-website|BlackHatWorth|7makemoneyonline)\.com [NC,OR]
        RewriteCond %{HTTP_REFERER} (?:lomb|lombia|econom|lumb)\.co [NC,OR]
        RewriteCond %{HTTP_REFERER} (?:cenoval|Iskalko)\.ru [NC,OR]
        RewriteCond %{HTTP_REFERER} (?:smailik|humanorightswatch)\.org [NC,OR]
        RewriteCond %{HTTP_REFERER} (?:ranksonic|savetubevideo)\.info [NC]
        RewriteRule ^ – [F]
    </IfModule>

# Set the server administrator email
    SetEnv SERVER_ADMIN admin@example.com


# Serve custom error pages
    ErrorDocument 400 /template/400.html
    ErrorDocument 401 /template/401.html
    ErrorDocument 403 /example.com/template/403.php
    ErrorDocument 404 /example.com/template/404.php
    ErrorDocument 500 /template/500.html

Thank you in advance for help.

Community
  • 1
  • 1
hamilton9
  • 33
  • 4
  • Why not just redirect to the base url like www.mysite.com? That should only show the base url and load index by default – VIDesignz Nov 27 '15 at 19:20
  • 1
    Currently, you have all requests that do not match a directory or file going to index.php to be processed. So you need to look there to handle how the slug, "example.com/home" is handled. You mention that you process a var "$path" to determine what to get from the database. So, check there and if it is the home slug, redirect the request to "example.com/", as VIdesignz suggests. If you want to avoid a redirect, look into HTML5's history API specification to handle it via JS. – Practically Nov 27 '15 at 19:28

1 Answers1

3

You need to remove the leading slash from your rule pattern, leading slash is not matched in htaccess context.

change this line :

RewriteRule ^/$ /home [L]

to

RewriteRule ^$ /home [L]

or another solution is DirectoryIndex

add this line above the "RewriteEngine On" directive

 DirectoryIndex /home

this will load

 example.com/

as

 example.com/home.
Amit Verma
  • 40,709
  • 21
  • 93
  • 115