【问题标题】:htaccess pretty urls setuphtaccess 漂亮的网址设置
【发布时间】:2012-01-10 01:53:02
【问题描述】:

我第一次使用 htaccess 为我的网站 html 文件和 1 个 php 文件制作漂亮的 url。我只是想知道我是否能够就我的 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。我还必须在每个文件的头部添加“base 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]

【问题讨论】:

  • htmlpage 现在实际上是否存在(存储)为? htmlpage.html(你只是想让它看起来像一个目录)或者现在是新文件htmlpage/index.html?您当前设置的困难 - AFAICT - 是您实际上拥有正确的 URL,您只想通过重定向和重写使它们看起来不同。当旧 URL 实际上不再有效时,事情会变得更容易,您可以将它们重写为最终的、更好的 URL。
  • htmlpage 存在并存储在根(子域)目录中 - 当您输入 /htmlpage/ 时,漂亮的 url 链接会加载 .htmlpage。我不确定如何使 .html 页面不起作用...但是如果访问者已经为 .html 页面添加了书签,那会是个坏主意吗?
  • 这不是破坏事物。但是,如果您基本上将“file.html”重写为“file”,然后让您的网络服务器在内部将其自动重写回“file.html”,那么当您搞砸时,您很容易陷入重定向循环。这就是为什么我询问您的“文件”(在服务器上)是否称为..dirs/somepage.html...dirs/somepage/index.html 或其他名称。与您要在其下提供服务的 URL 无关。
  • Jess:阅读您的其他 cmets 我不确定您是否需要从用户那里接收 URI phppage.php?p=1 并将其重写为 phppage/1,反之亦然。

标签: php html .htaccess url


【解决方案1】:

这一行

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

将发送一些页面到 phppage.php,即使它们看起来不像

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

因为在 RewriteRule 的第一个参数中没有提到 phppage。

【讨论】:

  • 哦,是的,我可以看到它没有改变 1,所以页面 /2/、/3/、/4/ 等都保持为 phppage/1/ :( 如果你不介意,什么我需要写信来改变这个吗?:S
  • RewriteRule ^subdomain/phppage/([^/\.]+)/?$ phppage.php?p=$1 [L]
【解决方案2】:

尝试将以下内容添加到域根目录中的 .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]  

【讨论】:

    猜你喜欢
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2012-06-08
    • 2012-11-06
    • 2015-10-04
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多