用mod_rewrite htaccess重写php url变量

时间:2017-01-22 20:15:33

标签: php .htaccess

我仍然在绕着htaccess工作,想要了解我的代码是否正确或需要一些工作。另外我知道有很多像这样的问题,但仍然无法弄清楚,如何将网址String[] values = csvStr1.split(","); Set<String> hashSet = new HashSet<String>(Arrays.asList(values)); String[] values2 = csvStr2.split(","); for (String value: values2 ) { if( hashSet.add(value) == true ) { //value already present. Ignore this or do whatever you want. } } 重写为days/content.php?day=mondays/mon?谢谢!

&#13;
&#13;
days/content/mon
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

如果您要将网址days/content.php?day=mon重写为days/mon,请考虑以下重写规则:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/days/content.php
RewriteCond %{QUERY_STRING} ^day=([a-zA-Z]+)$
RewriteRule ^(.*)$ /days/%1? [R,L]

或者如果您希望将其重写为days/content/mon,请考虑以下问题:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/days/content.php
RewriteCond %{QUERY_STRING} ^day=([a-zA-Z]+)$
RewriteRule ^(.*)$ /days/content/%1? [R,L]

希望它有所帮助!

答案 1 :(得分:1)

您需要使用的是:

RewriteEngine On
RewriteRule ^days/([^/]*)$ /days/content.php?day=$1 [L]

它将为您留下以下网址:www.example.com/days/mon

确保在测试之前清除缓存。

答案 2 :(得分:0)

RewriteCond %{SCRIPT_FILENAME} !-f    
RewriteRule ^days\/(.+)$ days/content.php?day=$1 [NC,L]

将从days/mon重定向到days/content.php?day=mon

如果您收到未找到错误,可能是因为RewriteBase /。默认情况下,.htaccess url是相对于它所在的目录。通过将基数设置为/,网址将变为相对于root。

对于下面的示例,我删除了RewriteBase

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On    

    # This shouldn't be required, as it defaults to the directory the .htaccess is in
    # RewriteBase /

    # rewrite days/[day] into days/content.php?day=[day]
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^days\/(.+)$ days/content.php?day=$1 [NC,L]

    ...
</IfModule>