【问题标题】:Want 'example.com/home' to show as 'example.com' in address bar希望“example.com/home”在地址栏中显示为“example.com”
【发布时间】:2016-03-02 00:08:14
【问题描述】:

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

我还有一个功能可以将任何未知的 slug 直接重定向到主页。如果我输入 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

提前感谢您的帮助。

【问题讨论】:

  • 为什么不直接重定向到像 www.mysite.com 这样的基本 url?默认情况下应该只显示基本 url 和加载索引
  • 当前,您有所有与要处理的目录或文件不匹配的请求到 index.php。因此,您需要查看那里以处理 slug“example.com/home”的处理方式。您提到您处理 var "$path" 以确定从数据库中获取什么。因此,请检查那里,如果它是 home slug,请按照 VIdesignz 的建议将请求重定向到“example.com/”。如果您想避免重定向,请查看 HTML5 的历史 API 规范以通过 JS 处理它。

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


【解决方案1】:

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

改变这一行:

RewriteRule ^/$ /home [L]

RewriteRule ^$ /home [L]

或者另一种解决方案是 DirectoryIndex

在“RewriteEngine On”指令上方添加这一行

 DirectoryIndex /home

这将加载

 example.com/

作为

 example.com/home.

【讨论】:

    猜你喜欢
    • 2011-12-13
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 2011-12-19
    • 2023-03-21
    相关资源
    最近更新 更多