想要'example.com/home'在地址栏中显示为'example.com'

时间:2015-11-27 18:50:27

标签: php .htaccess url mod-rewrite url-rewriting

我的主页内容是根据使用slug'home'对数据库的查询而提取的。例如,如果我在地址栏中输入example.com/home,它会根据看到'/ home'查询数据库并提取正确的内容(使用变量'$ path')。 那部分工作正常。

我还有一个函数可以将任何未知的slu it直接重定向到主页。如果我输入example.com/foo并且表中没有相应的'foo'记录,它将只提供index.php。这是功能:

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

        header('Location:home'); 
}

我看过this回答和this回答似乎很接近,但并没有让我得到我需要的东西。我希望'example.com/home'在地址栏中显示为'example.com'。

此外,这是我的.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

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

您需要从规则模式中删除前导斜杠,在htaccess上下文中不会匹配前导斜杠。

更改此行:

RewriteRule ^/$ /home [L]

RewriteRule ^$ /home [L]

或其他解决方案是DirectoryIndex

将此行添加到&#34; RewriteEngine On&#34;指令

 DirectoryIndex /home

这将加载

 example.com/

作为

 example.com/home.