简体   繁体   中英

from query to path in subdirectory HTACCESS

I've an https domain with WordPress installed in the root directory. In the subdirectory "/test/" i made a web application that works using GET; the path is something like that:

https://www.example.com/test/file.php?url=urlname&etc=etc

I need to transform it in:

https://www.example.com/test/urlname/?etc=etc

I also need a redirect from the first type url to the second one.

It's the first time that i have to edit an htaccess file, and after searching on the web i tried this code

RewriteRule ^/?test/([^/]+)/$ test/file.php?url=$1&%{QUERY_STRING} [L,QSA]
RewriteCond %{REQUEST_URI}  ^/test/file\.php$
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^/?test/file\.php$ /test/%1/?%{QUERY_STRING}  [L,R=301]

but it obviously doesn't work. Can someone help me?

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /test/file\.php\?url=([^\s&]+)(?:&(\S*))?\s [NC]
RewriteRule ^ /test/%1?%2 [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^test/([^/]+)/?$ test/file.php?url=$1 [L,QSA,NC]

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