URL Rewriting invisibly - How to prevent rewritten URL to appear in the address bar?

谁都会走 提交于 2019-12-24 09:48:15

问题


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]


来源:https://stackoverflow.com/questions/7053401/url-rewriting-invisibly-how-to-prevent-rewritten-url-to-appear-in-the-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!