简体   繁体   English

如何使用.htaccess进行简单的url重写

[英]How to do a simple url-rewrite with .htaccess

.htaccess file located in my local computer with this path localhost/test/ .htaccess文件位于我的本地计算机中,其路径为localhost / test /

How to write a redirect url like 如何写一个像这样的重定向URL

localhost/test/errorsample

to

localhost/test/index.php?req_dir=$var

in my index.php 在我的index.php中

<?php
    echo ($_GET['req_dir']);
?>

so it should show errorsample 所以应该显示errorsample

RewriteRule ^test/(.*)$ /test/index.php?req_dir=$1 [L,QSA]

but this depends on other rules in the .htaccess file, so in your case it might not work without changing other rules. 但这取决于.htaccess文件中的其他规则,因此,在您情况下,如果不更改其他规则,它可能无法工作。

Explanation of the rule: (.*) matches everything after test/ and is captured into the rewriting variable $1. 规则说明: (.*)匹配test/之后的所有内容,并被捕获到重写变量$ 1中。

If you want a 302 redirect (ie the server tells the browser to use a different file instead), use [L,R,QSA] instead. 如果要302重定向(即服务器告诉浏览器改用其他文件),请改用[L,R,QSA] Usually a rewrite is better than a redirect, because a redirect shows the rewritten url in the address bar. 通常,重写比重定向要好,因为重定向会在地址栏中显示重写的url。 If you worry about canonical pages (a SEO technique), a 302 redirect to the canonical page is better than a direct rewrite. 如果您担心规范页面(SEO技术),则将302重定向到规范页面比直接重写更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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