Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think there is a typo in the last line of your <code>rewrite</code> rules.</p> <pre><code>RewriteRule ^(.*)$ index.php/?u=$1 [L,QSA] </code></pre> <p>seems should be corrected to</p> <pre><code>RewriteRule ^(.*)$ index.php?u=$1 [L,QSA] </code></pre> <hr> <p>I see two questions in your post:</p> <ol> <li>You want to get the <strong><em>slash-to-underscore</em></strong> converted URL value.</li> <li>When someone types in <code>/hi/hello/year</code> the URL should be <strong><em>redirected</em></strong> to <code>/hi_hello_year</code>.</li> </ol> <p><strong>URL rewrite</strong> and <strong>redirect</strong> are two independent actions. </p> <h2>1. Slash-to-underscore conversion</h2> <p>You already have <code>$_GET['u']</code> variable which holds <code>/hi/hello/year</code>.<br> Simply <code>str_replace</code>ing it will give you the converted URI string. </p> <pre><code>&lt;? // 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, '_' ); ?&gt; </code></pre> <h2>2. Redirecting to a new URL</h2> <p>Someone who typed in <code>/hi/hello/year</code> should see a new <code>/hi_hello_year</code> URL in his/her browser.<br> This involves <code>header( "Location: ..." )</code>;</p> <pre><code>&lt;? $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. ?&gt; </code></pre> <h2>3. Combined</h2> <p>However, above redirection is based on the assumption that the server has <code>hi_hello_year</code> document, otherwise it could lead to an endless <code>rewrite</code>-<code>redirect</code> loop. Let's combine and add a safety measure.</p> <pre><code>&lt;? // 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(); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload