.htaccess漂亮的URL不包括index.php并添加尾随/到结尾

时间:2013-04-21 17:00:24

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

好的,我已经和它斗了好几个小时了。我有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]
    
  4. 有人可以帮助我将这3个脚本合并为一个通用的漂亮URL脚本

1 个答案:

答案 0 :(得分: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]