2014-12-04 64 views
2

我读过很多问题和答案,但我不能确定哪一个更好,或者如何使用这些扩展隐藏方式的组合。
我想要什么是有一个url重写像stackoverflow!所以我应该做些什么有这些规则:URL扩展隐藏:重写与重定向

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> 

回答

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> 
+0

非常感谢。它现在工作正常,除了第一个。我想'example.com/file.anyEXT'显示=> 404的内容,但是它对任何'/ file.anyEXT'都能正常工作,但对'/ index.php'不能。我想'example.com/index.php'显示404. – osyan 2014-12-06 15:30:32

+0

好的检查更新的代码。它现在会为'example.com/index.php'显示404。 – anubhava 2014-12-06 15:34:56

+0

nop! 'example.com/index.php' =>将其网址更改为'example.com',并且像'example.com/index.phpjj'这样的东西显示500不是404 – osyan 2014-12-06 16:03:06

0

我我不确定mod_rewrite是否能胜任你想做的事情。但我想你真正想要的是引导。这就是我猜stackoverflow.com也在使用什么。像最现代的网络应用程序

这意味着所有的请求都经历了像index.php这样的单个PHP脚本。

所以你会给你的index.php文件的请求路径,它处理请求并加载请求的页面。

这里有一个交代如何将其与PHP的工作:http://www.binpress.com/tutorial/php-bootstrapping-crash-course/146

如果你使用像Zend框架的一个框架,有适当的已经是引导整合。