所以我试图让mod_rewrite做一些不同的事情,但我并没有完全接受它。我想:
重写某些网址,如下所示:
/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>
现在我遇到的问题有两个:
首先,如果我尝试访问上面步骤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.
非常感谢您提供的任何有关正常工作的帮助。
答案 0 :(得分: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]
不确定这是否是最有效的方法,但它可以满足我的需求。以为我会在这里分享它,以防它帮助其他任何人。