1

我是这里的新手。我正在尝试清理我的链接。我尝试查看这里的一些答案。我确定我什至没有接近....!你能帮我解决这个问题吗?

目前我的链接结构是这样的......

xyz.com/viewgallery.php?cname=Colorado-Fall&pcaption=Touched-By-Light

但我希望观众看到这个链接xyz.com/viewgallery/Colorado-Fall/Touched-By-Light(这意味着重定向?)

然后重写回我的链接。(??这就是我的理解)......

所以我写...

检查是否为“this1”的实际请求

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /viewgallery.php\?cname=(.*)&pcaption=(.+)

# redirect to "this2"
RewriteRule ^viewgallery\.php /%1/%2/?%3 [R=301,L]

# now rewrite "this2" back to "this1"
RewriteRule ^(.*?)/(.+)$ /viewgallery.php?cname=$1&pcaption=$2 [L,QSA]

它是否正确?我敢肯定它看起来像胡言乱语..对此感到抱歉!...

谢谢你的帮助..

4

1 回答 1

1

通过启用 mod_rewrite 和 .htaccess httpd.conf,然后将此代码放在您.htaccessDOCUMENT_ROOT目录下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# externally redirect /viewgallery.php?cname=Colorado-Fall&pcaption=Touched-By-Light to 
# /viewgallery/Colorado-Fall/Touched-By-Light to 
# RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(viewgallery)\.php\?cname=([^&]+)&pcaption=([^&\s]+) [NC]
# RewriteRule ^ /%1/%2/%3? [L,R=301,NE]

# internally forward /viewgallery/Colorado-Fall/Touched-By-Light to 
# /viewgallery.php?cname=Colorado-Fall&pcaption=Touched-By-Light
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?cname=$2&pcaption=$3 [L,QSA]

解释:

  1. 规则 1 用于外部重定向以使您的 URL 更漂亮
  2. 它在 %1、%2、%3 中捕获“viewgallery”和参数 cname 和 pcaption 的值
  3. 此后,这些捕获的变量用于创建漂亮的 URL/viewgallery/%2/%3
  4. 规则 2 适用于内部前锋
  5. 从 URL:/viewgallery/Colorado-Fall/Touched-By-Light它捕获 3 个变量:“viewgallery”、“Colorado-Fall”和“Touched-By-Light”到 $1、$2、$3
  6. 捕获的变量用于构造真实的 URL:/viewgallery.php?cname=$2&pcaption=$3

更新:

# /viewgallery.php?cname=Colorado-Fall&pcaption=Poked to /photos/Colorado-Fall/Poked.jpg
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+viewgallery\.php\?cname=([^&])+&pcaption=([^&\s]+) [NC]
RewriteRule ^ /photos/%1/%2.jpg? [R=301,L]

RewriteRule ^photos/([^/]+)/([^.]+)\.jpg$ /viewgallery.php?cname=$1&pcaption=$2 [QSA,L,NC]
于 2013-07-04T05:35:58.363 回答