htaccess:删除扩展名,然后重定向到index.php?p =< page> [英] htaccess: remove extension, and redirect to index.php?p=<page>

查看:84
本文介绍了htaccess:删除扩展名,然后重定向到index.php?p =< page>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RewriteRule ^([a-zA-Z0-9]+)?$ index.php?page=$1 [L]

我有以前的代码可以重写ex:domain.com/about至domain.com/index.php?page=about

I've got the previous code that rewrites ex: domain.com/about to domain.com/index.php?page=about

现在,我想增加一种安全措施,以防万一有人在结尾处添加.html或.php并认为应该在那儿,我希望它删除扩展名,但仍要遵循我以前的规则。我希望此更改忽略该文件是否实际存在。如果文件存在,则仍然需要处理结尾。

So now, I want to add a measure of safety to that just in case someone adds .html or .php to the end thinking it should be there, I want it to remove the extension, but still follow my previous rule. I want this change to ignore if the file actually exists. If the file exists, it needs to still process the ending.

例如:

如果有人输入.php或about.html,我希望它删除扩展名,然后处理上面的重写规则剩下的内容。

if someone types in about.php or about.html, I want it to remove the extension, and then process whats left to the rewrite rule above.

因此about.php将重写为about和然后重写为index.php?page = about

So about.php will rewrite to about and then rewrite to index.php?page=about

如果有可能,我希望它实际上将URL更改为domain.com/about。现在,上面的改写离开了domain.com/about,但是只是通过index.php处理它。如果他们输入about.php或about.html,它将把浏览器重定向到domain.com/about,我会喜欢的。

If at all possible, I'd like for it to actually change the URL to domain.com/about. Right now, the above rewrite leaves the domain.com/about but just processes it through index.php. I would love it if they enter about.php or about.html it would redirect the browser to domain.com/about .

有什么办法可以做到这一点?我一直在寻找使用htaccess删除文件扩展名的方法,但是它们都不符合重定向到index.php?page =的要求。他们都想以不同的方式重定向。

Any way possible to achieve this? I've looked around at removing file extension with htaccess, but none of them do what I want with redirecting to index.php?page=. They all want to redirect differently.

我需要这是通用的。它不能只处理关于,我需要它来处理特定地点中的任何东西。

I need this to be universal. It can't just process 'about', I need it to process anything in that specific spot.

推荐答案

目录或文件,并且具有扩展名,然后重定向而不扩展名:

If this is not a directory or a file, and it has an extension, then redirect without extension:

RewriteEngine On
RewriteBase /

RewriteRule   ^([a-zA-Z0-9]+)?$   index.php?page=$1 [L]

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

这篇关于htaccess:删除扩展名,然后重定向到index.php?p =< page>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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