Apache 如何让mod_rewrite同时删除文件扩展名和重定向某些URL?

Apache 如何让mod_rewrite同时删除文件扩展名和重定向某些URL?,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,所以我试图让mod_rewrite做一些不同的事情,但我不太同意。我想: 从URL中删除文件扩展名(在本例中为.shtml) 重写某些URL,如下所示: /dashboard -> /ui/dashboard/index.shtml /dashboard/ -> /ui/dashboard/index.shtml /dashboard/list -> /ui/dashboard/list.shtml /dashboard/list/ -> /ui/

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

  • 从URL中删除文件扩展名(在本例中为.shtml)
  • 重写某些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
    
  • 添加或删除尾部斜杠(我不在乎是哪个,只要它是一致的)

  • 我现在所拥有的东西让我有了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>
    
    如何获取这些index.shtml页面以保持地址栏的一致性

    第二,当我试图访问一个重写目录中的目录索引以外的页面时,我包含了一个尾随斜杠,它会给我一个404错误。例如:

    /dashboard/list/
    
    抛出404错误:

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

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

    因此,我找到了一种适合我需要的方法。以下是我提出的.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]
    
    不确定这是否是最有效的方法,但它符合我的需要。我想我会在这里分享它,以防它对其他人有帮助。

    RewriteCond%{REQUEST\u URI}!(\.[a-zA-Z0-9]{1,5}|/|#(.*))$与不匹配。导致错误号2的是shtml;%{REQUEST_URI}可能不是您需要匹配的对象,请尝试匹配%{SCRIPT_NAME}
    # 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]