【问题标题】:.htaccess pretty URL's excluding index.php and adding trailing / to the end.htaccess 漂亮的 URL 不包括 index.php 并在末尾添加尾随 /
【发布时间】:2013-04-14 13:37:35
【问题描述】:

好吧,我已经和它战斗了好几个小时了。我有 3 个不同的 .htaccess 脚本可以满足我的需要,但我无法将它们混合在一起。

  1. 从(example.com/gallery.php -> example.com/gallery)制作一个漂亮的网址

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9]+)$ $1.php
    
  2. #1 中的脚本将 example.com/index.php 转发到 example.com/index,因此此代码删除了 index.php,因此 example.com/index.php -> example.com

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L,QSA]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
    RewriteRule ^ /%1 [R=301,L]
    
  3. 脚本应添加尾部斜杠,因此 example.com/gallery -> example.com/gallery/

    # invoke rewrite engine
    RewriteEngine On
    RewriteBase /~new/
    
    # add trailing slash if missing
    rewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]
    

谁能帮我把这 3 个脚本组合成一个通用的漂亮 URL 脚本

【问题讨论】:

    标签: .htaccess mod-rewrite friendly-url trailing-slash pretty-urls


    【解决方案1】:

    将这些指令添加到您网站根目录的 .htaccess 中

    Options +FollowSymLinks
    RewriteEngine On
    
    # add trailing slash
    
    RewriteCond %{REQUEST_URI} !(/$|\.) 
    RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 
    
    # rewrite gallery/ to gallery.php
    
    RewriteCond %{REQUEST_URI} /$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ $1.php [L]
    
    # redirect example.com/index.php to example.com/
    
    RewriteCond %{REQUEST_URI} index\.php$
    RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]
    

    【讨论】:

    • 非常感谢,这工作正常,但它增加了另一个问题。它不再加载图像和样式,它进入页面,但没有加载任何内容,它只显示“空图片缩略图”和来自 html 的文本,我认为这是因为页面现在认为图像在 example.com/ gallery/images/ 虽然它们确实在 example.com/images/ 中,但有什么办法可以解决吗?
    • 你应该为图片和样式使用绝对路径
    • 你的意思是像“example.com/images/image.jpg”而不是“images/image.jpg”,我在本地更新网站,所以它会造成一些小问题......无论如何那个?
    • 无论如何,我认为我可以使用基本 url,但它仍然无法在 IE 中加载样式,我最终会弄清楚的。无论如何 tnx 再次 Amine,我还将你添加到我正在建立的网站上的荣誉名单中:ljis17.com/honorary_list
    猜你喜欢
    • 2023-03-12
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2021-03-09
    • 1970-01-01
    • 2017-06-15
    相关资源
    最近更新 更多