2011-11-30 79 views
1

我第一次使用htaccess为我的网站html文件和1个php文件制作漂亮的网址。我只是想知道如果我能够得到一些关于我的htaccess文件设置的建议,并且如果我设置它是一个好方法?因为我写的东西,我讨厌我的网站在某些情况下不工作。 :(htaccess漂亮的网址设置

示例HTML文件:

before: http://www.domain.com/subdomain/htmlpage.html 
after: http://www.domain.com/subdomain/htmlpage/ 

单一的PHP文件:

before: http://www.domain.com/subdomain/phppage.php?p=1 
after: http://www.domain.com/subdomain/phppage/1/ 

我已经在规则中加入的index.html重定向到index.php我也不得不在每个文件的头部添加“基本href”,因为我们使用相对链接

htaccess的文件:

Options +FollowSymLinks 
RewriteEngine on 
RewriteRule ^index\.html?$/[NC,R,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301] 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.+)/$ $1.html [L] 
RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L] 
+0

是'htmlpage'现在实际上存在(存储)作为? 'htmlpage.html'(你只是想让它看起来像一个目录),或者是现在的新文件'htmlpage/index.html'? 当前设置中的困难 - AFAICT - 是您实际上正确更正了通过重定向和重写使您看起来不同的URL。当旧的URL实际上不再有效时,事情会变得更加容易,并且您可以将它们重写为最终更好的URL。 –

+0

htmlpage存在并存储在根(子域)目录中 - 当您在/ htmlpage /中键入时,漂亮的url链接加载.htmlpage。我不确定如何使.html页面无法正常工作......但如果访问者已经为书签添加了一个.html页面,那么这会是一个坏主意吗? – Jess

+0

这不是关于打破事情。但是,如果您基本上将“file.html”重写为“file”,然后让您的web服务器自动重写为“file.html”,那么当您惹起某些问题时,您可以轻松地结束重定向循环。 这就是为什么我问你的'文件'(在服务器上)是否叫'..dirs/somepage.html','... dirs/somepage/index.html'或其他什么。独立于您想要在其下进行服务的URL。 –

回答

1

此行

RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L] 

会发送一些网页,以phppage.php即使他们不喜欢看

http://www.domain.com/subdomain/phppage/1/ 

因为在第一个参数没有phppage的提重写规则。

+0

哦,是的,我可以看到它不会改变1,所以pages/2 /,/ 3 /,/ 4/etc都保持为phppage/1/:(如果你不介意,我需要写什么来改变这个?:S – Jess

+1

RewriteRule^subdomain/phppage /([^/\.]+)/?$ phppage.php?p = $ 1 [L] –

+0

非常感谢!它现在可以正常工作!:D – Jess

1

尝试添加在你的域的根目录下,以您的.htaccess文件

Options +FollowSymLinks 

RewriteEngine On 
RewriteBase/

# redirect http://www.domain.com/subdomain/htmlpage.html to http://www.domain.com/subdomain/htmlpage/ 
RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.html$ [NC] 
RewriteRule . %1/ [L,R=301] 


#redirect http://www.domain.com/subdomain/phppage.php?p=1 to http://www.domain.com/subdomain/phppage/1/ 
RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.php$ [NC] 
# or alternatively if page is literally phppage uncomment below and comment above 
#RewriteCond %{REQUEST_URI} ^(/subdomain/phppage)\.php$ [NC] 
RewriteRule . %1/ [L,R=301] 

#if url does not end with/
RewriteCond %{REQUEST_URI} ^(/subdomain/.+[^/])$ [NC] 
# and its not for an existing file 
RewriteCond %{REQUEST_FILENAME} !-f 
# put one trailing slash 
RewriteRule . %1/ [L,R=301] 

#write http://www.domain.com/subdomain/htmlpage/ to htmlpage.html 
RewriteCond %{REQUEST_URI} ^/subdomain/([^/]+)/$ [NC] 
RewriteRule . %1.html [L] 


#write http://www.domain.com/subdomain/phppage/1/ to phppage.php?p=1 
RewriteCond %{REQUEST_URI} ^/subdomain/[^/]+/([0-9]+)/$ [NC] 
RewriteRule . phppage.php?p=%1 [L]