0

好吧,我已经和它打了几个小时。我有 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 脚本吗

4

1 回答 1

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]
于 2013-04-21T20:12:10.260 回答