【问题标题】:.htaccess rewriting rule rewriting css files.htaccess 重写规则重写css文件
【发布时间】:2015-12-25 06:07:37
【问题描述】:

我有一个重写规则来制作漂亮的 url,但它也重写 css、js 和其他文件。

我想重写

http://localhost/example/post/?postid=af1ub4zu4y&title=this-is-the-first-post.html

http://localhost/example/post/af1ub4zu4y/this-is-the-first-post.html

我正在尝试这种方式

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/([^/]*)/(.*\.html) /example/post/?postid=$1&title=$2 [QSA,L,B]

也试过这样的

RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^post/([^/]*)/(.*\.html) /example/post/?postid=$1&title=$2 [QSA,L,B]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
RewriteRule ^post/([^/]*)/(.*\.html) /example/post/?postid=$1&title=$2 [QSA,L,B]

但是这些解决方案都不适合我。请查看并提出任何可行的方法。

【问题讨论】:

  • 你想重写什么?提供样品。另外,请确认htaccess是否正在加载; mod-rewrite 已启用等,
  • @hjpotter92 请看更新,htaccess 已加载,mod-rewrite 已启用
  • 是的,我收到 404 错误
  • @TallboY 我将删除下面的回复,因为您可能会对这两种方法感到困惑。
  • @hjpotter92 我没有感到困惑,我一次尝试一种解决方案,无论如何感谢您花时间提供帮助

标签: apache .htaccess mod-rewrite url-rewriting friendly-url


【解决方案1】:

试着把这条规则放在/example/post/.htaccess:

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /example/post/

RewriteRule ^([^/]+)/([^/.]+\.html?)$ ?postid=$1&title=$2 [QSA,L,B,NC]

【讨论】:

  • 我也有这条规则,它运行良好RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^view/([^/]*)$ /example/view/?hash=$1 [QSA,L]
  • 很可能 OP 已经同时测试了我们的两种解决方案!我会删除我的回复。
  • 我将 .htaccess 放入 /example/post/.htaccess 并且仍然损坏 css
  • http://localhost/example/post/af1ub4zu4y/this-is-the-first-post.html 导致 CSS 损坏。
  • http://localhost/example/post/?postid=af1ub4zu4y&title=this-is-the-first-post.h‌​tml 正在工作,但没有从 postidtitle 获取值并重定向到 http://localhost/example/post/?postid=af1ub4zu4y&title=this-is-the-first-post.h%E2%80%8C%E2%80%8B%E2%80%8C%E2%80%8Btml
猜你喜欢
  • 2012-02-27
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多