Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 重写规则以获得更好的URL_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Apache 重写规则以获得更好的URL

Apache 重写规则以获得更好的URL,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我想像这样重定向查询 www.example.com/mysite/post/3/title-of-the-post 到 通过在www.example.com/mysite/的根目录下设置.htaccess文件。我试过: .htaccess file --------------- RewriteEngine On RewriteRule ^post/([0-9]+)/* index.php?id=$1 [L] 为什么这样不行在这种情况下如何设置重写规则?我是否需要在.htaccess中

我想像这样重定向查询

www.example.com/mysite/post/3/title-of-the-post

通过在
www.example.com/mysite/
的根目录下设置
.htaccess
文件。我试过:

.htaccess file
---------------

RewriteEngine On
RewriteRule ^post/([0-9]+)/* index.php?id=$1 [L] 

为什么这样不行在这种情况下如何设置重写规则?我是否需要在.htaccess中添加比这一行更多的行?

您不需要在结尾处添加
*
(实际上应该是
*
),因为表达式的结尾不是
$
。因此,您可以这样做:

RewriteRule ^post/([0-9]+)/ index.php?id=$1 [L]
如果要关闭表达式,请使用以下命令:

RewriteRule ^post/([0-9]+)/.*$ index.php?id=$1 [L]

您的服务器是否启用了
mod_rewrite
?我认为您的规则中缺少了一个
$
,请尝试
RewriteRule^post/([0-9]+)/.$index.php?id=$1[L]
是@IkoTikashi:
a2enmod rewrite
模块重写已启用
谢谢,它几乎可以工作了(我必须在缺少的上添加
重写引擎):页面现在已加载,但是现在找不到CSS和JS文件,因为它们是在
http://www.example.com/mysite/post/3/css/mysite.css
;)而不是
http://www.example.com/mysite/css/mysite.css
。。。如何允许CSS、JS文件进入正确的位置?JS文件等的问题与@MikeAnthony相同:它们在
example.com/mysite/post/3/blah.JS
中搜索,而不是
example.com/mysite/blah.JS
。如何解决此问题?@Basj有关详细信息,请参阅标签信息。它链接到,这将回答这个问题。你需要确保你的文件被正确引用。因此,样式表的参考应该是:
。或者,您应该设置一个基本标记:
并保持其他一切不变。@MikeAnthony没有其他解决方案比硬编码每个东西更好,比如
/mysite/css/mysite.css
(现在我有了
css/mysite.css
)。。。想象有一天
/mysite/
变成
/blah/
,然后我必须改变一切
RewriteRule ^post/([0-9]+)/.*$ index.php?id=$1 [L]