htaccess重定向问题

发布于 2024-09-15 12:38:57 字数 338 浏览 2 评论 0原文

我有一个用 php 编写的网站。

地址如下:
http://www.site.com/page.php

我愿意任何请求:
www.site.com/page.php

www.site.com/page

前往:
www.site.com/page/

(并强制输入www。)

有人可以帮助我吗?

I have a site I've written in php.

The addresses are as follows:
http://www.site.com/page.php

I'd like any request to:
www.site.com/page.php
or
www.site.com/page

To go to:
www.site.com/page/

(And force a www.)

Can anyone help me?

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

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

发布评论

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

评论(4

梦回梦里 2024-09-22 12:38:57

对于域:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

对于您的第一个目录:

RewriteRule ^/([^/]*)(\.php)? /$1/

For domain :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

For your first directory :

RewriteRule ^/([^/]*)(\.php)? /$1/
荒岛晴空 2024-09-22 12:38:57
RewriteRule ^page.php$ http://www.site.com/page/ [QSA,L]
RewriteRule ^page$ http://www.site.com/page/ [QSA,L]

也许这会起作用。

RewriteRule ^page.php$ http://www.site.com/page/ [QSA,L]
RewriteRule ^page$ http://www.site.com/page/ [QSA,L]

Maybe this will work.

执笔绘流年 2024-09-22 12:38:57

这应该做你想要的:

RewriteEngine On

RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?)\.php    [OR]
RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST}   !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]

# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php

This should do what you wanted:

RewriteEngine On

RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?)\.php    [OR]
RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST}   !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]

# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
请叫√我孤独 2024-09-22 12:38:57

蒂姆的答案是最接近的,但它也确实在图像和 css 中添加了尾部斜杠...

因此,采用蒂姆的答案,并稍微编辑它,我们会得到:

RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png)$ - [L]

RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?)\.php    [OR]
RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST}   !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]

# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php

哪个应该有效!

Tim's answer is the closest, but it also does adds the trailing slash to images and css...

So taking Tim's answer, and slightly editing it gives us:

RewriteEngine on

RewriteRule \.(css|jpe?g|gif|png)$ - [L]

RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?)\.php    [OR]
RewriteCond %{THE_REQUEST}  ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST}   !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]

# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php

Which should work!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文