【问题标题】:Linux Mint - Apache 2.4 .htaccess rewriteRule does not workLinux Mint - Apache 2.4 .htaccess rewriteRule 不起作用
【发布时间】:2016-08-16 00:54:40
【问题描述】:

我有一个网站使用 .htaccess 重写规则,但 soem 不起作用。

这是我的 .htaccess 文件:

RewriteEngine On
RewriteBase /
# Inicio
RewriteRule ^inicio|home|index$ index.php [QSA,L]

# Paginacion amigable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.([0-9]+)$ $1?pag=$2 [QSA,L]

# Miniaturas
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*\/)?tdd([A-Za-z0-9_~-]*)\/(.*)$ imagen.php?param=$2&src=$1$3 [QSA,L]

#paginas
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*-p([0-9]+)$ interna.php?id=$1 [QSA]

#Intranet
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*-i([0-9]+)$ intranet.php?id=$1 [QSA]

#publicaciones
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*-n([0-9]+)$ publicaciones_detalle.php?id=$1 [QSA]

# Paginas sin extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ $1.php [QSA]

# sitemap
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule sitemap.xml sitemap.php [QSA]

# Buscador
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search$ buscar.php [QSA]



# En caso de no existencia
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ 404.php [QSA]

# Paginas incluidas en el template
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} ^.*\.php$
RewriteRule ^(.*)$ plantilla.php?uri=$1 [QSA]

但我有一个错误,例如当我访问 www.domain.com/contacto 它向我抛出了这个错误:

Not Found

The requested URL /contacto was not found on this server.

但如果我像这样 www.domain.com/contacto.php 更改网址,它会起作用。

我认为底部列表中的这条规则应该可以完成工作,不是吗?

# Paginas sin extension
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+)$ $1.php [QSA]

为什么我做错了?

这些是为我的 apache 加载的所有模块:

Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 php7_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)

这是我在 /etc/apache2/sites-enabled 上的 domain.dev.conf 文件:

<VirtualHost *:80>
    ServerAdmin webmaster@domain.dev
    DocumentRoot /var/www/html/domain/
    ServerAlias www.domain.dev
    ServerName domain.dev
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/html/domain>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                RewriteEngine On
    </Directory>
</VirtualHost>

在 apache 2.2 上运行良好,但升级到 2.4 后停止运行。

apache 2.和2.4在应用htaccess文件的形式上有区别吗

谢谢

【问题讨论】:

  • planntilla.php 规则块的目的是什么?目前表明,如果您请求 /foo 和 /foo 存在并且请求文件名是 /.*.php 然后重写为 plantilla.php - 这是没有意义的。
  • Plantilla.php 就像一个模板。所有其他页面都在该文件中回显

标签: php apache .htaccess mod-rewrite


【解决方案1】:

添加此规则以处理 .php。

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

【讨论】:

    【解决方案2】:

    更改此块:

    # Paginas sin extension
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+)$ $1.php [QSA]
    

    到这里:

    # Paginas sin extension
    # (if exists)
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+)$ $1.php [QSA,L]
    

    为了增强和遵守 DRY principle 的目的,请将整个 .htaccess 文件替换为以下代码:

    RewriteEngine On
    RewriteBase /
    
    # Ignorar archivos y directorios
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [END]
    
    # Inicio
    RewriteRule ^inicio|home|index$ index.php [QSA,L]
    
    # Paginacion amigable
    RewriteRule ^(.+).(\d+)$ $1?pag=$2 [QSA,L]
    
    # Miniaturas
    RewriteRule ^(.*/)?tdd([A-Za-z0-9_~-]*)/(.*)$ imagen.php?param=$2&src=$1$3 [QSA,L]
    
    # paginas
    RewriteRule ^.*-p(\d+)$ interna.php?id=$1 [QSA,L]
    
    # Intranet
    RewriteRule ^.*-i(\d+)$ intranet.php?id=$1 [QSA,L]
    
    # Publicaciones
    RewriteRule ^.*-n(\d+)$ publicaciones_detalle.php?id=$1 [QSA,L]
    
    # sitemap
    RewriteRule ^sitemap.xml$ sitemap.php [QSA,L]
    
    # Buscador
    RewriteRule ^search$ buscar.php [QSA,L]
    
    # Paginas incluidas en el template
    # RewriteCond %{REQUEST_FILENAME} -f
    # RewriteCond %{REQUEST_FILENAME} ^.*\.php$
    # RewriteRule ^(.*)$ plantilla.php?uri=$1 [QSA]
    
    # Paginas sin extension
    # (En caso de existencia)
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+)$ $1.php [QSA,L]
    
    # En caso de no existencia
    RewriteRule ^.*$ 404.php [QSA]
    

    在那里,我已经删除了所有对检查请求是否与现有文件或文件夹匹配的条件的重复引用——您只需要这样做一次,然后在匹配时停止重写。为了安全起见,我建议为目录关闭它(通过删除第 6 行:RewriteCond %{REQUEST_FILENAME} -d)。

    如您所见,我已将我在对您的原始问题的评论中提到的那个块注释掉,因为它似乎没有特定的功能 - 您需要修复它才能使其工作。

    【讨论】:

    • 您好,感谢您的回复。我已经尝试过了,但我首先收到 404 错误。我没有在 apache 2.2 上得到它,它工作正常。现在我已经更新到 2.4 并停止工作
    猜你喜欢
    • 2012-12-30
    • 2016-04-06
    • 1970-01-01
    • 2022-01-23
    • 2014-09-03
    • 2012-12-10
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多