从查询到子目录HTACCESS中的路径

时间:2015-10-27 11:23:52

标签: php apache .htaccess mod-rewrite

我在根目录中安装了一个带有WordPress的https域。 在子目录“/ test /”中,我创建了一个使用GET工作的Web应用程序;路径是这样的:

  

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

我需要将其转换为:

  

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

我还需要从第一个类型网址重定向到第二个网址。

这是我第一次编辑htaccess文件,在网上搜索后我尝试了这段代码

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]

但它显然不起作用。 有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

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]