【问题标题】:Writing clean urls with .htaccess使用 .htaccess 编写干净的 url
【发布时间】:2016-04-24 06:25:31
【问题描述】:

更新 - 好的,谢谢大家的测试,真的很有帮助。代码很好。我的问题是没有绝对的“/”链接。 (由于从 public_html 文件夹移动)。

我现在只有 2 个问题。

1.) 将“^achilles”替换为“^([0-9a-zA-Z_-]+)”,缩略图不会出现(参数数据没有携带???)

2.) 我怎样才能让http://funkygames.co/games/achilles/achilles/1 变得更短像http://funkygames.co/games/achilles 或者参数是绝对强制性的。

# Turn Rewrite Engine On
RewriteEngine on     

# Set the base to /games/ so we need not include it in the rules
RewriteBase /games/

#Rewrite for achilles.php?games_path=xxxxxxxxxx.yyy&category_id=zzz
RewriteRule ^achilles/(.*)/([0-9]+) $1.php?games_path=$1.swf&category_id=$2 [NC,L]

我在哪里学到的:www.youtube.com/watch?v=1pbAV6AU99I


已解决!谢谢你们。 解决方案:

# Turn Rewrite Engine On
RewriteEngine on     

# Set the base to /games/ so we need not include it in the rules
RewriteBase /games/

#Rewrite for achilles.php?games_path=xxxxxxxxxx.yyy&category_id=zzz
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+) $1.php?games_path=$1.swf&category_id=$2 [NC,L]

www.funkygames.co/games/achilles/1

【问题讨论】:

  • 游戏文件夹必须在 public_html 文件夹中。不确定您的措辞是否是这种情况。

标签: php .htaccess url url-rewriting clean-urls


【解决方案1】:

可以在public_html/htaccess中使用如下代码

# Turn Rewrite Engine On
RewriteEngine on

#Rewrite for achilles.php?games_path=xxxxxxxxxxxxx&category_id=x
RewriteRule ^(?:(achilles|games)/)?([0-9a-zA-Z_-]+)/([0-9]+)/?$ /achilles.php?games_path=$1.swf&category_id=$2 [NC,L]

这将重写:

  • /achilles.php?games_path=achilles.swf&category_id=1

【讨论】:

  • Not Found The requested URL /achilles/achilles/1 was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. 没用,但看起来很有趣。也许如果你告诉我什么是“?”、“:”和“$”,我可以重新安排周围的东西,看看是否发生了什么。
【解决方案2】:

games 文件夹中存储的.htaccess 文件中,插入以下内容:

RewriteEngine On

# Set the base to /games/ so we need not include it in the rules

RewriteBase /games/

# Rewrite {something}/{id} to {something}.php?games_path={something}&category_id={id}
# Examples: achilles/1 -> achilles.php?games_path=achilles.swf&category_id=1
#           other/3 -> other.php?games_path=other.swf&category_id=3

RewriteRule ^([0-9a-z]+)/(\d+)/?$ $1.php?games_path=$1&category_id=$2 [NC,L]

在此示例中,尾部斜杠是可选的。如果您不想要尾部斜杠,请从请求模式中删除 /?。此外,此示例假定请求模式的第一部分将匹配现有的 PHP 文件。如果您只想使用achilles.php,请将$1.php 替换为achilles.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多