【问题标题】:.htaccess rewrite help, remove .php from URL.htaccess 重写帮助,从 URL 中删除 .php
【发布时间】:2013-11-08 18:37:49
【问题描述】:

我的 mod_rewrite 没有遇到另一个问题,首先,时间是我的 .htaccess 文件:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^news/([0-9]+)/?$ /news?id=$1 [L,QSA]
RewriteRule ^contact/([0-9]+)/?$ /contact?do=$1 [L,QSA]
RewriteRule ^account/([a-zA-Z]+)/?$ /account?action=$1 [L,QSA]
RewriteRule ^admin/([a-zA-Z]+)/?$ /admin?action=$1 [L,QSA]
RewriteRule ^login/([a-zA-Z]+)/?$ /login?action=$1 [L,QSA]
RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L] <- This line doesn't work
RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [L,QSA]

除了 RewriteRule ^([^.]+)$ $1.php [NC,L] 之外,所有行都正常工作 当我使用这条线时,所有其他规则都会被打断。

我做错了什么?

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    您需要在有问题的规则中使用L 标志。

    对于删除 .php 扩展名,这是更好的规则:

    Options +FollowSymlinks -MultiViews
    
    RewriteEngine on
    
    ## If the request is for a valid directory
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    ## If the request is for a valid file
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    ## If the request is for a valid link
    RewriteCond %{REQUEST_FILENAME} -l
    ## don't do anything
    RewriteRule ^ - [L]
    
    RewriteRule ^news/([0-9]+)/?$ /news?id=$1 [L,QSA]
    RewriteRule ^contact/([0-9]+)/?$ /contact?do=$1 [L,QSA]
    RewriteRule ^account/([a-zA-Z]+)/?$ /account?action=$1 [L,QSA]
    RewriteRule ^admin/([a-zA-Z]+)/?$ /admin?action=$1 [L,QSA]
    RewriteRule ^login/([a-zA-Z]+)/?$ /login?action=$1 [L,QSA]
    RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
    

    【讨论】:

    • 不起作用,它不会显示主页,某些页面可以正常工作,而我要删除 .php 的页面也无法正常工作
    • www.domain.com 不起作用,www.domain.com/about 起作用,www.domain.com/api/file 不起作用,www.domain.com/api/ file.php 确实有效,前 2 个 URL 是通过索引路由的,正如您在 .htaccess 文件中看到的那样:)
    • 它应该显示 home.phtml,about 链接也路由到 about.phtml 有效
    • 1.你想隐藏.php 而不是.phtml。 2. 显示home.phtml for www.domain.com 的代码在哪里?
    • 当您访问 domain.com 时,索引会路由到 /templates/home.phtml,如果我访问 domain.com/about,那么规则会将其设为 domain.com/index.php?page =关于,它在索引中都是通过 PHP 路由的,.phtml 由于路由而被删除,并且规则:RewriteRule ^([\w\d~%.:_\-]+)$ index.php? page=$1 [L,QSA] 这样做它不需要说 index.php?page=about,而只是 .domain.com/about,所以没有 .phtml,并且在网站上,要么没有.php,但我在站点上有一个API服务,并且不包含在路由中,所以我需要放置api/file.php并且我想删除.php
    【解决方案2】:

    试试类似的东西

    #Preventing loop 
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
    RewriteRule (.*)\.php$ $1 [R=301]
    
    RewriteCond %{REQUEST_URI} !^.+\.php.*$
    RewriteRule .* - [L]
    

    把这个放在RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]之后

    【讨论】:

    • 当我这样做时,页面将进入一个永无止境的循环:/
    • 见上文,我修好了小费
    猜你喜欢
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多