URL重写代码无法在我的.htaccess上运行

时间:2018-03-15 10:10:20

标签: php .htaccess url url-rewriting url-rewrite-module

我的原始网址

  

http://www.mcoh.co.in/blog-single?category=Blog&id=Janurary%202018%20Product%20training%20Scores

并期待:

  

http://www.mcoh.co.in/mcohBlog/Janurary%202018%20Product%20training%20Scores

但是我收到了与原始相同的网址。

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^mcoh([^/]*)/([^/]*)\$ /blog-single?category=$1&id=$2 [L]

3 个答案:

答案 0 :(得分:0)

你对这一行的问题:

RewriteRule ^mcoh([^/]*)/([^/]*)\.php$ /blog-single?category=$1&id=$2 [L]

将其替换为:

RewriteRule ^mcoh([^/]*)/([^/]*)$ /blog-single?category=$1&id=$2 [L]

此扩展程序已被先前规则删除。

此外,您的代码应如下所示:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^mcoh([^/]*)/([^/]*)$ /blog-single?category=$1&id=$2 [L,NE]

答案 1 :(得分:0)

RewriteEngine on
RewriteRule ^/?mcohBlog/([^/d]+)/?$ blog-single.php?category=Blog&id=$1 [L,QSA]

你能试试吗?只需在代码末尾添加此重写符。并删除你的最后一行代码

修改

您的代码组织也是非常错误的。这就是为什么它没有按预期工作的原因

Options +FollowSymLinks -MultiViews     
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^/?mcohBlog/([^/d]+)/?$ blog-single.php?category=Blog&id=$1 [L,QSA]

这应该是你的最后一个代码

答案 2 :(得分:0)

重定向/重写

http://www.mcoh.co.in/blog-single?category=Blog&id=Janurary%202018%20Product%20training%20Score

http://www.mcoh.co.in/mcohBlog/Janurary%202018%20Product%20training%20Scores

将以下内容放在htaccess文件的顶部

RewriteEngine on

#To externally redirect /blog-single?category=foo&id=bar to /mochBlog/bar
RewriteCond %{QUERY_STRING} ^category=([^&]+)&id=(.+)$ [NC]
RewriteRule ^.*$ /mochBlog/%2? [R,L]
#To internally redirect /mochBlog/bar to /blog-single?category=foo&id=bar
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^mochBlog/(.+)$ /blog-single?category=blog&id=$1 [L]