的.htaccess删除.php然后迫使斜杠 [英] .htaccess remove .php and force trailing slash

查看:576
本文介绍了的.htaccess删除.php然后迫使斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的根文件夹有文件。 jobs.php contact.php

I have files in my root folder. jobs.php, contact.php.

我希望能够使用的.htaccess删除的.php 扩展,并迫使斜线 / 。这样, www.domain.com/jobs / www.domain.com/contact / 进入到各自的页。

I want to be able to use .htaccess to remove the .php extension and force a trailing slash /. So that, www.domain.com/jobs/ and www.domain.com/contact/ goes to their respective pages.

另外,我想 jobs.domain.com 指向 www.domain.com/jobs /

<IfModule mod_rewrite.c>
    Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /

    RewriteRule ^(rosquillos)/$ $1 [L,R=301,NC]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
</IfModule>

这是我有这么远了,像我试试。我似乎无法得到它的工作。

This is what I've got so far, and as much as I try. I can't seem to get it to work.

任何帮助将是真棒。

编辑:

我想改一下我的问题。现有的code删除的.php 扩展对我来说,我真的只需要现在是指向子域,以正确的文件的附加规则:即。 jobs.domain.com 点jobs.php。我可以生活在没有斜线。

I would like to rephrase my question. The existing code removes the .php extensions for me, what I really only need right now is an additional rule that points the subdomain to the correct file: ie. jobs.domain.com points to jobs.php. I can live without the trailing slash.

推荐答案

您的条件:

RewriteCond %{REQUEST_FILENAME}\.php -f

会失败,如果您的网址的结尾加上一个结尾的斜线。但是,如果你只有工作联系人那么你忍不住会想:

will fail if your URL's end with a trailing slash. But if you only have jobs and contact then you'll just want:

RewriteEngine On

# add trailing slash and redirect browser
RewriteRule ^([^/]+)$ /$1/ [L,R=301]

RewriteRule ^([^/]+)/$ /$1.php [L]

# redirect direct access to php files:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)\.php
RewriteRule ^ /%1/ [L,R=301]

哦,我忘了:

Oh and I forgot:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]

假设 *。domain.com 具有相同的文档根目录为 www.domain.com

Assuming that *.domain.com has the same document root as www.domain.com.

这篇关于的.htaccess删除.php然后迫使斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆