URL扩展隐藏:重写与重定向

时间:2014-12-04 08:31:12

标签: apache .htaccess mod-rewrite

我已经阅读了很多问题和答案,但我无法确定哪一个更好或如何使用这些扩展隐藏方式的组合。
我想要的是要像 stackoverflow 那样重写网址!那么我还应该做些什么来制定这些规则:

url: example.com/file.anyEXT...  show content of => 404
url: example.com/unknown-Cat...  show content of => 404

url: example.com/cat1            show content of => example.com/cat1/index.php
url: example.com/cat1/index.php  show content of => 404
url: example.com/cat1/any...any  show content of => 404
url: example.com/cat1/11/title   show content of => example.com/cat1/single.php  (post id 11)

但我的htaccess文件只是这样:

url: example.com/file.anyEXT     
     show content of => example.com/index.php  (should show 404)

url: example.com/unknown-Cat
     show content of => example.com/index.php  (should show 404)

url: example.com/unknown-Cat/
    show broken content of => index.php => cant load css and js => (should show 404)

url: example.com/file.anyEXT/
     show broken content of => index.php => cant load css and js => (should show 404)

url: example.com/cat1 
     show content of => example.com/cat1/index.php (works fine!)

url: example.com/cat1/index.php
     show content of => example.com/cat1/index.php (should show 404)

url: example.com/cat1/any...any
     show content of => 404  (works fine!)

url: example.com/cat1/11/title
     show content of => example.com/cat1/single.php  (post id 11) (works fine!)

我有什么

mydomain-|
         |
         |
         |__cat1-|__.htaccess  // cat1 htaccess
         |       |__index.php
         |       |__single.php
         |
         |__cat2-|__.htaccess // cat2 htaccess
                 |       |__index.php
                 |       |__single.php
                ...
                 |__.htaccess  // root htaccess
                 |__index.php  // home-page 

我的htaccess基于大多数建议的方式:

<IfModule mod_rewrite.c>

# Enable Rewrite Engine
RewriteEngine On
RewriteBase /

<files ".htaccess">
order allow,deny
deny from all
</files>    
Options All -Indexes

# Redirect from www.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

</IfModule>

这是我在我的根文件夹和所有子文件夹中使用的,我有两个文件index.phpsingle.php,我在每个子文件夹中都有.htaccess:

Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /cat1/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+)/(.+?)/?$ single.php?id=$1&title=$2 [L,QSA]

</IfModule>

2 个答案:

答案 0 :(得分:1)

您可以使用:

<强> DocumentRoot/.htaccess

<IfModule mod_rewrite.c>

<files ".htaccess">
order allow,deny
deny from all
</files>    
Options All -Indexes

RewriteEngine On
RewriteBase /

# Redirect from www.
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,NE,R=301]

# 404 for index.php in URL
RewriteCond %{THE_REQUEST} /index\.php[\s?/] [NC]
RewriteRule ^ - [L,R=404]

</IfModule>

<强> /cat1/.htaccess

Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /cat1/

RewriteCond %{THE_REQUEST} /index\.php[?\s] [NC]
RewriteRule ^index\.php$ - [L,R=404]

RewriteRule ^(\d+)/([^/]+)/?$ single.php?id=$1&title=$2 [L,QSA]

</IfModule>

答案 1 :(得分:0)

我不确定mod_rewrite是否能够满足您的要求。但我猜你真正想要的是自举。这就是我猜的stackoverflow.com也在使用的内容。像最现代的Web应用程序一样

这意味着所有请求都通过单个PHP脚本,如index.php。

因此,您将向index.php文件提供请求的路径,并处理请求并加载请求的页面。

以下是对如何使用PHP的解释:http://www.binpress.com/tutorial/php-bootstrapping-crash-course/146

如果您使用像Zend Framework这样的框架,那么已经正确地集成了引导程序。

相关问题