Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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

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
Regex 用斜杠重写URL_Regex_.htaccess - Fatal编程技术网

Regex 用斜杠重写URL

Regex 用斜杠重写URL,regex,.htaccess,Regex,.htaccess,如果输入http://someone.com/hi/hello/yeah我希望结果是http://someone.com/?u=hi_hello_yeah 这是我到目前为止写的,它只替换没有斜杠“/”的url 而且,我想知道如果用户输入http://someone.com/hi/hello/yeah,它将重定向到http://someone.com/hi_hello_yeah这很棘手,因为它需要递归应用重写规则。 通过httpd.conf启用mod_rewrite和.htaccess,然后将此代

如果输入
http://someone.com/hi/hello/yeah
我希望结果是
http://someone.com/?u=hi_hello_yeah

这是我到目前为止写的,它只替换没有斜杠“/”的url


而且,我想知道如果用户输入
http://someone.com/hi/hello/yeah
,它将重定向到
http://someone.com/hi_hello_yeah

这很棘手,因为它需要递归应用重写规则。

通过
httpd.conf
启用mod_rewrite和.htaccess,然后将此代码放入
文档根目录下的
.htaccess

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# this recursive rule will be applied as many times as the /s in the URI
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.+)$ /$1_$2 [L]

# this rule will be applied once all /s have been replaced by _
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /?u=$1 [L,QSA]

我想你的
重写
规则的最后一行有一个输入错误

RewriteRule ^(.*)$ index.php/?u=$1 [L,QSA]
似乎应该更正为

RewriteRule ^(.*)$ index.php?u=$1 [L,QSA]

我在你的帖子中看到两个问题:

  • 您希望获取转换后的URL值,该值由斜杠到下划线
  • 当有人键入
    /hi/hello/year
    时,URL应重定向到
    /hi\u hello\u year
  • URL重写重定向是两个独立的操作

    1.斜杠到下划线的转换 您已经有了
    $\u GET['u']
    变量,该变量包含
    /hi/hello/year

    简单地
    str\u replace
    ing它将为您提供转换后的URI字符串

    <?
    // this is the landing index.php page specified in the last rewrite rule. 
    
    // should be "/hi/hello/year" for "/hi/hello/year" URL request. 
    $rewritten_uri = isset($_GET['u']) ? $_GET['u'] : '';
    
    // converting slashes to underscores.
    $converted_uri = str_replace( '/', '_', $rewritten_uri );
    
    // the string begins and/or ends with a slash, so remove it.
    $ready_to_use_uri = trim( $converted_uri, '_' );
    
    ?>
    
    3.合二为一 但是,上述重定向基于以下假设:服务器具有
    hi\u hello\u year
    文档,否则可能会导致无休止的
    重写
    -
    重定向
    循环。让我们合并并添加一个安全措施

    <?
    // this is the landing index.php page specified in the last rewrite rule. 
    
    // should be "/hi/hello/year" for "/hi/hello/year" URL request. 
    $rewritten_uri = isset($_GET['u']) ? $_GET['u'] : '';
    
    // converting slashes to underscores.
    $converted_uri = str_replace( '/', '_', $rewritten_uri );
    
    // the string begins and/or ends with a slash, so remove it.
    $ready_to_use_uri = trim( $converted_uri, '_' );
    
    // redirect only when such file exists 
    if ( file_exist( $ready_to_use_uri ) )
    {
      header( 'Location: /' . $ready_to_use_uri );
      exit(); // unless you have some more work to do.
    }
    
    header("HTTP/1.0 404 Not Found");
    echo "The document '" . $ready_to_use_uri . "' is not found on this server";
    exit();
    ?>
    

    链接是否总是像
    http://someone.com/.../...
    ?它可以使用
    .info
    .net
    .gov
    ?或者最坏的情况是使用类似ip的
    192.168.10.123:8080/…
    <?
    $new_url = '/' . $ready_to_use_uri; // which came from the above code
    header( 'Location: ' . $new_url );
    exit(); // unless you have some more work to do.
    ?>
    
    <?
    // this is the landing index.php page specified in the last rewrite rule. 
    
    // should be "/hi/hello/year" for "/hi/hello/year" URL request. 
    $rewritten_uri = isset($_GET['u']) ? $_GET['u'] : '';
    
    // converting slashes to underscores.
    $converted_uri = str_replace( '/', '_', $rewritten_uri );
    
    // the string begins and/or ends with a slash, so remove it.
    $ready_to_use_uri = trim( $converted_uri, '_' );
    
    // redirect only when such file exists 
    if ( file_exist( $ready_to_use_uri ) )
    {
      header( 'Location: /' . $ready_to_use_uri );
      exit(); // unless you have some more work to do.
    }
    
    header("HTTP/1.0 404 Not Found");
    echo "The document '" . $ready_to_use_uri . "' is not found on this server";
    exit();
    ?>