简体   繁体   English

mod重写不能在.htaccess中工作

[英]mod rewrite not working in .htaccess

I am trying to write rewriterule for php site. 我正在尝试为php网站编写rewriterule。

My URL is like http://example.com/attorneys?pid=69 我的网址就像http://example.com/attorneys?pid=69

I write in .htacess as below: 我用.htacess写如下:

RewriteEngine On
RewriteRule ^attorneys/([0-9]+)/?$ attorneys&pid=$1 [NC,L] 

Both the link example.com/attorneys?pid=69 and example.com/attorneys/69 works. 链接example.com/attorneys?pid=69example.com/attorneys/69有效。

How can I make the browser know that if it get the first link it have to show the second one in browser. 如何让浏览器知道如果它获得第一个链接,它必须在浏览器中显示第二个链接。

RewriteRule ^attorneys/([0-9]+)/?$ attorneys&pid=$1 [NC,L,R=302]

So you want to redirect http://xyz.com/attorneys?pid=69 to http://xyz.com/attorneys/69 ? 所以你想将http://xyz.com/attorneys?pid=69重定向到http://xyz.com/attorneys/69 Another rule after(!) the first rule should do the trick: 在(!)第一条规则之后的另一条规则应该可以解决问题:

RewriteEngine On
RewriteRule ^attorneys/([0-9]+)/?$ attorneys&pid=$1 [NC,L] 
RewriteRule ^attorneys&pid=([0-9]+)$ attorneys/$1 [NC,L,R=301]

Because the first rule is marked with the L flag, the second won't be executed if the first matches. 因为第一个规则标有L标志,所以如果第一个匹配则不会执行第二个规则。 (See the documentation of mod_rewrite flags here .) (请参阅此处的mod_rewrite标志文档 。)

First, I need to say you there is no need to do that. 首先,我需要说你没有必要这样做。 Anyway, I was forced to do it in the past for a SEO-maniac client. 无论如何,我曾被迫为一个SEO疯子客户做过。 I tell you, that's not an elegant solution! 我告诉你,这不是一个优雅的解决方案!

On top of the attorneys PHP page (I don't know if it's a directory with index.php or not, but you know it) add this code: attorneys PHP页面之上(我不知道它是否是index.php的目录,但你知道吗)添加以下代码:

    // Get request script
    $request = preg_split('/[\\/?&]/', $_SERVER['REQUEST_URI']);
    $request_script = $request[1];

    // Check old URL
    if ($request_script == 'attorneys') {

        // Redirect
        header('Location: /attorneys/' . $_GET['id'];
        exit();

    }

Maybe it's not exactly like this for your case, but I hope you get the mechanism. 也许它对你的情况不完全是这样,但我希望你得到这个机制。

This appears to be a simple ask. 这似乎是一个简单的问题。 Try this code in your .htaccess file under DOCUMENT_ROOT (and comment out your existing code): 在DOCUMENT_ROOT下的.htaccess文件中尝试此代码(并注释掉现有代码):

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteOptions MaxRedirects=5

RewriteRule ^(attorneys)/([^/]+)/?$ $1?pid=$2 [NC,L]

RewriteCond %{QUERY_STRING} ^pid=(.*)$ [NC]
RewriteRule ^(attorneys)/?$ /$1/%1? [NC,L,R=301]

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

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