自定义.htaccess重写规则

时间:2012-01-08 02:15:51

标签: php wordpress .htaccess mod-rewrite

我想将所有内容重定向到一个脚本,例如index.php?url=inputurl

使用if / else我想解析url

index.php中的

在我的自定义表

中运行查询url
  • 如果 url是mach:echo“ok”
  • 其他什么都不做

如何在Wordpress的根文件夹中设置.htaccess?

示例:

custom_table中的网址:

  • ASD
  • DFG
  • GHJ

如果用户放置:

www.mysite.com/asd

- > mod_rewrite应输出:www.mysite.com/index.php?url = asd

其他如果用户提出:

www.mysite.com/zzz

- >什么都不做

2 个答案:

答案 0 :(得分:2)

我认为以下.htaccess应该可以解决问题:

RewriteEngine On

# Redirects everything that is not index.php to index.php
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php?url=$1 [L,R]

编辑:在重写时不包含您的文件夹和文件(如/ js,/ css等),在RewriteRule行之前添加以下行(请参阅注释):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

在PHP脚本中:

$url = $_GET['url'];

// the method is_valid should check if the page exists in DB
if (is_valid($url)) {
    // do something here
    // maybe redirect with header('Location: path')
} else {
    // show a not found page (error 404)
}

答案 1 :(得分:2)

您希望能够从数据库中读取数据,并且如果不匹配则不执行任何操作。

这将要求您运行代码来访问db然后返回到apache进行处理,并且无法从.htacccess(尽管它来自httpd.conf)。

.htaccess解决方案是内联指定所有“表格”条目,如下所示。

RewriteEngine on
RewriteBase /

#if asd or dfg or ghj
RewriteCond %{REQUEST_URI} ^/(asd|dfg|ghj) [NC]
RewriteRule . index.php?url=%{REQUEST_URI} [L]
相关问题