0

所以我试图让 mod_rewrite 做一些不同的事情,但我并不完全同意它。我想:

  1. 从 URL 中删除文件扩展名(在本例中为 .shtml)
  2. 像这样重写某些 URL:

    /dashboard       -> /ui/dashboard/index.shtml
    /dashboard/      -> /ui/dashboard/index.shtml
    /dashboard/list  -> /ui/dashboard/list.shtml
    /dashboard/list/ -> /ui/dashboard/list.shtml
    /workspace       -> /ui/workspace/index.shtml
    /workspace/      -> /ui/workspace/index.shtml
    /account/manage  -> /ui/account/manage.shtml
    /account/manage/ -> /ui/account/manage.shtml
    
  3. 添加或删除尾部斜杠(我不在乎,只要它是一致的)

我目前拥有的东西让我完成了大约 90% 的路程。在我的 .htaccess 文件中,我有以下内容:

DirectoryIndex index.shtml index.html index.htm

<IfModule mod_rewrite.c>

  Options +FollowSymlinks
  RewriteEngine On
  RewriteBase /

  # Get rid of the /ui/ in the URLs
  RewriteRule ^(account|workspace|dashboard)([a-zA-Z0-9\-_\.\/]+)?$ /ui/$1$2 [NC,L]

  # Add the trailing slash
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

  # Remove the shtml extension
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}.shtml -f
  RewriteRule ^([^\.]+)/$ $1\.shtml

</IfModule>

现在我遇到的问题是双重的:

首先,如果我尝试访问上面第 2 步中列出的目录中列出的索引页面之一,只要我使用尾部斜杠,就可以,但如果我省略尾部斜杠,则 URL 会错误地重写(页面仍然加载,但是)。例如

/dashboard/   remains /dashboard/ in the address bar.
/dashboard    rewrites to /ui/dashboard/ in the address bar.

如何让这些 index.shtml 页面保持地址栏一致?

其次,当我尝试访问其中一个重写目录中的目录索引以外的页面时,并且我包含一个尾部斜杠,它会给我一个 404 错误。例如:

/dashboard/list/

抛出 404 错误:

The requested URL /ui/dashboard/list.shtml/ was not found on this server.

非常感谢您提供的任何帮助以使其正常工作。

4

1 回答 1

2

所以我想出了一种适合我需要的方法。这是我想出的 .htaccess ,内联评论:

# Match URLs that aren't a file
RewriteCond %{REQUEST_FILENAME} !-f

# nor a directory
RewriteCond %{REQUEST_FILENAME} !-d

# if it's the index page of the directory we want, show that and go no further
RewriteRule ^(account|workspace|dashboard)/?$ /ui/$1/index.shtml [L]

# If we've gotten here, we're dealing with something other than the directory index.
# Let's remove the trailing slash internally
# This takes care of my second issue in my original question
RewriteRule ^(.*)/$ $1 [L]

# Do the rewrite for the the non-directory-index files. 
RewriteRule ^(account|workspace|dashboard)([a-zA-Z0-9\-_\.\/]+)?$ /ui/$1$2 [L]

不确定这是否是最有效的方法,但它可以满足我的需求。以为我会在这里分享它,以防它帮助其他人。

于 2012-05-24T22:00:13.660 回答