【问题标题】:htaccess to redirect url's that contain somethinghtaccess 重定向包含某些内容的 url
【发布时间】:2012-05-05 15:04:31
【问题描述】:

我正在尝试找出如何在 htaccess 中做这样的事情:

#This address links to a place that does not exist
localhost/prefix/games/userid/game-name

默默地(我猜是[L])重定向到

#This folder has an index.php, so we're good!
localhost/prefix/protected/files/userid/game-name/

我知道我可以有一个链接:

localhost/prefix/games/index.php?user=userid&title=game-name

只需在 index.php 中添加一些重定向...

但我不希望 url 参数在我网站上的链接中可见...所以我想它确实需要是 htaccess。

我必须承认,我读过 htaccess 的内容,但它让我无法理解,因为没有人以我理解的方式解释。

任何帮助将不胜感激。

编辑::///

RewriteEngine On        
RewriteBase /prefix

# stop directory listings
Options -Indexes 

#stops htaccess views
<Files .htaccess>
order allow,deny
deny from all
</Files>

# remove .php; use THE_REQUEST to prevent infinite loops
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
    RewriteRule (.*)\.php$ $1 [R=301]         

# remove index
    RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /$
    RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1\.php [L]    

# redirect game folders
    RewriteRule ^prefix/games/(.*)$ /prefix/protected/files/$1/ [R=301]  

【问题讨论】:

    标签: .htaccess url redirect parameters


    【解决方案1】:

    你可以拥有

    RewriteRule ^prefix/games/(.*)$ prefix/protected/files/$1 [L]
    

    将所有 http://localhost/prefix/games/userid/game-name 链接重定向到 http://localhost/prefix/protected/files/userid/game-name

    或者,你可以拥有

    RewriteCond %{QUERY_STRING} user=(.*)&title=(.*)$
    RewriteRule ^prefix/games/index.php$ /prefix/protected/files/%1/%2? [L]
    

    移动所有链接,例如:http://localhost/prefix/games/index.php?user=userid&amp;title=game-name 到 http://localhost/prefix/protected/files/userid/game-name

    【讨论】:

    • 看第一个:^=host/domain,(.*)=把这部分存入一个变量,$1调用变量集?
    • ^ => 在 domain 之后开始,剩下的两个假设是正确的。
    • 您的好先生,非常有帮助。我会加倍努力自己做下一个。滴答!
    • 如果我将 RewriteRule ^prefix/games/(.*)$ prefix/protected/files/$1 [L] 更改为 [R=301],则 URL 应重写为 localhost/prefix/protected/files/userid/game-name。或者我是否需要一个 RewriteCond。可以举个例子吗?
    • 是的,应该。我自己从来没有使用过[R] 规则。你可以在这里查看你的重写规则:htaccess madewithlove
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 2011-03-14
    • 2018-05-20
    • 1970-01-01
    • 2012-11-04
    • 2014-05-24
    相关资源
    最近更新 更多