URL extension hiding : rewrite vs redirect

落爺英雄遲暮 提交于 2019-12-12 20:08:32

问题


I have read lots of question and answers but i cant decide which one is better or how to use a combination of these ways of extension hiding.
what i want is to have a url rewriting like stackoverflow! so what else should i do to have these rules:

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)

but my htaccess files just do like these:

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

What i have

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

my htaccess based on most suggested way:

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

It is what i used in my root folder and in all my sub-folders i have two files index.php and single.php and i have .htaccess like this in every sub folders:

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:


You can use:

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>



回答2:


I am not sure if mod_rewrite is even capable of what you want to do. But I guess that what you really want is bootstrapping. And this is what I guess what stackoverflow.com is also using. Like the most modern web applications

This means all request are going through a single PHP script like index.php.

So you will give the path of the request to your index.php file and it process the request and load the page requested.

Here is an explaination how it can work with PHP: http://www.binpress.com/tutorial/php-bootstrapping-crash-course/146

If you use a Framework like Zend Framework, there is properly already a bootstrapping integrated.



来源:https://stackoverflow.com/questions/27289388/url-extension-hiding-rewrite-vs-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!