的.htaccess:重写GET语义网址,模仿文件夹 [英] .htaccess: Rewrite GET as semantic URL and emulate folder

查看:135
本文介绍了的.htaccess:重写GET语义网址,模仿文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重写这一点:

  • mysite.com/ 专辑> mysite.com/albums.php
  • mysite.com/ 专辑/ > mysite.com/albums.php
  • mysite.com/ 专辑/富 /专辑/富/ > mysite.com/ 专辑/ foo.html (检查下一行)
  • mysite.com/ 专辑/ foo.html > mysite.com/albums.php?album=foo
  • mysite.com/ 专辑/富/条 /专辑/富/酒吧/ > mysite.com/ 专辑/富/一个bar.html (检查下一行)
  • mysite.com/ 专辑/富/一个bar.html > mysite.com/albums.php?album=foo&photo=bar
  • mysite.com/albums > mysite.com/albums.php
  • mysite.com/albums/ > mysite.com/albums.php
  • mysite.com/albums/foo or /albums/foo/ > mysite.com/albums/foo.html (check next line)
  • mysite.com/albums/foo.html > mysite.com/albums.php?album=foo
  • mysite.com/albums/foo/bar or /albums/foo/bar/ > mysite.com/albums/foo/bar.html (check next line)
  • mysite.com/albums/foo/bar.html > mysite.com/albums.php?album=foo&photo=bar

目前的.htaccess 至今:

RewriteEngine on
RewriteRule /albums/(.*)/(.*)/ albums.php?album=$1&photo=$2
RewriteRule albums/(.*)/(.*) albums.php?album=$1&photo=$2
RewriteRule /albums/(.*)/ albums.php?album=$1
RewriteRule albums/(.*) albums.php?album=$1
RewriteRule /albums/ albums.php

不过是有错误的一塌糊涂,怎么可以这样工作呢?

But is a mess with errors, how can this work well?

注意这必须工作只是与 /albums.php ,不能与其他的.php 的index.php /contact.php 等

NOTE: This must work just with /albums.php, not with other .php files like /index.php, /contact.php, etc.

推荐答案

您可以在您的根目录的.htaccess尝试这些规则:

You can try these rules in your root .htaccess:

Options -MultiViews
RewriteEngine on

RewriteRule ^albums/([\w-]+)/([\w-]+)(?:\.html|/)?$ albums.php?album=$1&photo=$2 [L,QSA,NC]

RewriteRule ^albums/([\w-]+)(?:\.html|/)?$ albums.php?album=$1 [L,QSA,NC]

RewriteRule ^albums(?:\.html|/)?$ albums.php [L,NC]

这篇关于的.htaccess:重写GET语义网址,模仿文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆