Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache URL扩展隐藏:重写与重定向_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

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

Apache URL扩展隐藏:重写与重定向,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我已经阅读了很多问题和答案,但我无法决定哪一个更好,或者如何结合使用这些扩展隐藏方式。 我想要的是像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 co

我已经阅读了很多问题和答案,但我无法决定哪一个更好,或者如何结合使用这些扩展隐藏方式。
我想要的是像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>

我不确定mod_rewrite是否能够实现您想要的功能。但我想你真正想要的是引导。这就是我猜stackoverflow.com也在使用的东西。就像最现代的web应用程序一样

这意味着所有请求都要通过一个PHP脚本,如index.PHP

因此,您将给出index.php文件的请求路径,它将处理请求并加载请求的页面

下面解释了如何使用PHP:

如果您使用像Zend Framework这样的框架,则已经正确地集成了引导功能。

您可以使用:

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

非常感谢。除了第一个,它现在工作正常。我希望
example.com/file.anyEXT
显示=>404的内容,但是它对任何
/file.anyEXT
都可以正常工作,但对
/index.php
则不行。我想
example.com/index.php
显示404.ok检查更新的代码。它将显示404 for
example.com/index.php
now.nop
example.com/index.php
=>将其url更改为
example.com
,并且类似于
example.com/index.phpjj
的内容显示了500个非404oops。我在一个新浏览器中得到了500个非404oops,这里的日志是:由于可能的配置错误,一个请求超过了10个内部重定向的限制……我提出了一个新问题,因为这个要求与这个问题没有什么不同。如果可以的话,我会尽力回答。
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>