简体   繁体   English

apache RewriteRule不起作用

[英]apache RewriteRule not working

I have this url http://www.example.com/Courses/get/38789/my-course , i added this rule to the .htaccess file 我有这个网址http://www.example.com/Courses/get/38789/my-course ,我已将此规则添加到.htaccess文件中

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/Courses/get
RewriteRule ^Courses/get/(.*)/(.*)$ course-$1-$2 [R=301,L]

but when i go to http://www.example.com/Courses/get/38789/my-course nothing happens, i stay on the same page, there is no redirect. 但是当我转到http://www.example.com/Courses/get/38789/my-course时,什么也没发生,我停留在同一页面上,没有重定向。

ps the link is just an example not the actual link PS链接只是一个例子,而不是实际的链接

A more efficient method would be to use the following: 一种更有效的方法是使用以下方法:

RewriteRule ^Courses/get/(\d+)/([^/]+)/?$ /course-$1-$2 [R=301,L]

Now, keep in mind that this rule should come before any rules that may rewrite the request to, say, an index.php file. 现在,请记住,此规则应先于任何可能将请求重写为index.php文件的规则。 This would be naturally true if the code you posted in your question was all of your code. 如果您在问题中发布的代码就是您的所有代码,那么自然就是如此。 If not, please post your entire .htaccess file so we can be sure it is being placed in the right location. 如果没有,请发布整个.htaccess文件,这样我们可以确保将其放置在正确的位置。

Be sure the mod_rewrite is turned on, and the you have set AllowOverride All in your virtual host/Apache site configuration. 确保已打开mod_rewrite ,并且已在虚拟主机/ Apache站点配置中设置了AllowOverride All If you're running on a shared production server, this would not apply to you. 如果您在共享生产服务器上运行,那么这将不适用于您。

Side note: Whilst it does work, you need not use RewriteEngine on twice - only once at the beginning of your file will suffice. 旁注:虽然它确实可以工作,但是您无需两次使用RewriteEngine on文件开头只需一次就足够了。 You also do not need RewriteCond %{REQUEST_URI} ^/Courses/get - it is essentially redundant as you are already using an expression to test against the request. 您也不需要RewriteCond %{REQUEST_URI} ^/Courses/get它实际上是多余的,因为您已经在使用表达式对请求进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM