怎么做?虚荣 URL 和忽略带有 .htaccess 的 .PHP 扩展名

发布于 2024-12-22 14:05:17 字数 1397 浏览 0 评论 0原文

我已经让以下 .htaccess 完美工作:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]

这将允许我编写这样的 url:

http://www.mysite.com/peter  =>  http://www.mysite.com/mypage.php?u=peter

但是,如果我想要有一个附加规则来避免为现有文件编写 .php 扩展名,该怎么办?

http://www.mysite.com/contact   (contact.php exists on file)

我尝试将其添加到我的 curr htaccess 中(但不起作用):

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

换句话说,我希望拥有虚荣网址并忽略同一 .htaccess 中的 .php 扩展名。


解决了! 感谢 Ulrich Palha 和 Mario(不知道顺序和 LAST [L] 标志!

RewriteEngine on
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]

I've got the following .htaccess working perfectly:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]

This will allow me to write urls like this:

http://www.mysite.com/peter  =>  http://www.mysite.com/mypage.php?u=peter

But what if I want have an aditional rule to avoid writing the .php extension for existing files?

http://www.mysite.com/contact   (contact.php exists on file)

I tried adding this to my curr htaccess (but doesnt work):

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

In other words, I would like to have vanity urls and ignore the .php extension in the same .htaccess.


SOLVED!
Thanks to Ulrich Palha and Mario (didnt know about the ordering and the LAST [L] flag!

RewriteEngine on
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世蒼涼 2024-12-29 14:05:17

这是一个订购的问题。 RewriteRules 按照 .htaccess 中标注的顺序应用
请参阅所有内容您曾经想了解 Mod_Rewrite 规则但又不敢问吗?

只需添加新块作为第一个重写语句,添加 [LAST] 标志,它就应该可以工作。

该规则无条件工作(不受任何 RewriteCond 保护),因此应始终是所有 RewriteRules 的最后一个:(

 RewriteRule ^(.*)$ ...

并且所有前面的 RewriteRules 最好应带有 [L] 标志。您有一个解决方法那里。)

It's a matter of ordering. RewriteRules are applied in the order they are noted in the .htaccess
See Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?

Just add your new block as first rewrite statement, add a [LAST] flag, and it should work.

This rule works unconditionally (not guarded by any RewriteCond), so should always be the last of all RewriteRules:

 RewriteRule ^(.*)$ ...

(And all preceeding RewriteRules should preferrably carry a [L] flag. You have a bit of a workaround there.)

夏见 2024-12-29 14:05:17

对我来说,这段代码有效:

在“/etc/apache2/sites-available/default”或“httpd.conf”中,

您必须(至少)在默认目录

...
    <Directory />
        ...
        Options FollowSymLinks
        AllowOverride All
        ...
    </Directory>
...

或在站点特定站点目录中

...
    <Directory "/var/www/my-site">
        ...
        Options FollowSymLinks
        AllowOverride All
        ...
    </Directory>
...

,并在站点目录中创建或编辑“.htacces”(例如“/var/www/my-site/.htaccess”)并添加这段代码:

AddType application/x-httpd-php htm html php
AddHandler application/x-httpd-php .htm .html

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

For me this code work:

in "/etc/apache2/sites-available/default" or "httpd.conf"

you must have (at least) these options in the default directory

...
    <Directory />
        ...
        Options FollowSymLinks
        AllowOverride All
        ...
    </Directory>
...

OR in the site specific site directory

...
    <Directory "/var/www/my-site">
        ...
        Options FollowSymLinks
        AllowOverride All
        ...
    </Directory>
...

and create or edit the ".htacces" in the directory of the site (for example "/var/www/my-site/.htaccess") and add this code:

AddType application/x-httpd-php htm html php
AddHandler application/x-httpd-php .htm .html

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文