使用.htaccess

时间:2016-01-18 22:57:01

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

更新 - 好的,谢谢大家的测试,这真的很有帮助。代码很好。我的问题不是绝对的" /"链接。 (由于从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

2 个答案:

答案 0 :(得分:0)

您可以在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

答案 1 :(得分:0)

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

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