简体   繁体   中英

Reverse mod_rewrite in htaccess

Is there a way to change the url with .htaccess but don't make a redirect?

I have links like

<a href="?p=hello_world">Hello World</a>

in my script (dir/index.php) and want to change the url to url.de/dir/hello_world .

My current .htaccess file:

 RewriteEngine On
 RewriteCond %{QUERY_STRING} ^p=(.*)$
 RewriteRule ^(.*)$ /%1? [R,L]  
 RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?p=$1 [L]
 RewriteRule ^([a-zA-Z0-9-]+)$ index.php?p=$1 [L]

but now from localhost/dir/?p=hello_world the url changed to localhost/hello_world without /dir . And I don't know the name of the directory because everyone should be able to use my project without changing this .htaccess on his server.

You need to use this code:

RewriteEngine On

# Determine the RewriteBase dynamically (\2 is backreference for string after 1st slash
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]

# External redirect from /dir?p=hello_world to /dir/hello_world
RewriteCond %{THE_REQUEST} \?p=([^\s&]+) [NC]
RewriteRule ^ %{ENV:BASE}%1? [R=302,L]

# Internal rewrite from /dir/hello_world to /dir/index.php?p=hello_world
RewriteRule ^([a-zA-Z0-9-]+)/?$ %{ENV:BASE}index.php?p=$1 [L,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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