URL Rewriting invisibly - How to prevent rewritten

2019-08-06 05:31发布

问题:

I have browsed the other topics, including this one: Mod_rewrite invisibly: works when target is a file, not when it's a directory, but I can't find a solution to my problem.

I have the following rewriting rule:

RewriteRule ^([a-zA-Z0-9_-]+)$ ./index.php?s=$1 [L,NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ ./index.php?s=$1 [L,NC]

What it does is to write anything like http://myaddress/parameter to http://myaddress/index.php?s=parameter and show this new rewritten address in the browser's address bar.

How can I make rewriting without showing the rewritten URL in the address bar?


Edit

This is the content of my .htaccess file:

DirectoryIndex index.php

RewriteEngine On

RewriteRule ^([a-z0-9_\-]+)/?$ index.php?s=$1 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?$ index.php?u=$1&s=$2 [L,NC]
RewriteRule ^([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?([a-zA-Z0-9_-]+)\/?$ index.php?u1=$1&u2=$2&s=$3 [L,NC]

回答1:

1. No need for 2 rules that do the same job (the only difference is presence of trailing slash).

2. No need to have a-zA-Z in pattern if you have [NC] flag -- a-z is enough.

3. Try rule without ./

Considering all the above mentioned the rule will become:

RewriteRule ^([a-z0-9_\-]+)/?$ index.php?s=$1 [L,NC,QSA]

P.S. I have also added the QSA flag to preserve original query string (if present).

The rule is tested and is working fine. If it still does not work for you then post ALL rewrite rules that you have.



回答2:

This should work:

RewriteEngine On

RewriteRule ^([-a-zA-Z0-9_]+)$ index.php?s=$1 [L]
RewriteRule ^/?$ index.php [L]