.htaccess:修改 URL

发布于 2025-01-01 17:51:06 字数 424 浏览 0 评论 0原文

我将网站的大部分公共内容保存在一个名为“pages”的文件夹中,其中每个页面都有一个文件夹,然后在每个页面中我都有针对每种内容类型的文件夹。

编辑:重写它以使其更容易理解。

所以我的典型 URL 是: http://www.example.co.uk/folderName/folderName/folderName/fileName.php

但我希望它显示如下: http:// /www.example.co.uk/fileName

我还需要 URL 来处理粘在其末尾的 PHP 变量,如下所示:http://www.example.co.uk /fileName?phpVar=1

我知道这是可能的,因为我看到到处都是这样做的。 :L

I keep most of my website's public content in a folder named "pages", inside there I have a folder for each page, then inside each of those I have folders for each type of content.

Edit: Rewrote it to be more understandable.

So my typical URL is: http://www.example.co.uk/folderName/folderName/folderName/fileName.php

But I would like it to display like this: http://www.example.co.uk/fileName

I would also need to URL to be work with PHP variables stuck on the end of it, like so: http://www.example.co.uk/fileName?phpVar=1

I know it's possible, as I see it done everywhere. :L

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

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

发布评论

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

评论(2

无人问我粥可暖 2025-01-08 17:51:06

也许是这个?

# Turn mod_rewrite on
RewriteEngine On
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite /filename to /pages/filename/pages/filename.php
RewriteRule ^(.+)$ /pages/$1/pages/$1.php [L]

Maybe this?

# Turn mod_rewrite on
RewriteEngine On
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite /filename to /pages/filename/pages/filename.php
RewriteRule ^(.+)$ /pages/$1/pages/$1.php [L]
楠木可依 2025-01-08 17:51:06
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^fileName1/?$ folderName/folderName/folderName/fileName1.php [L]
RewriteRule ^fileName2/?$ folderName/folderName/folderName/fileName2.php [L]

应该做你正在寻找的事情。假设您希望用户将 http://example.com/about.php 视为 http://example.com/about

添加 RewriteCond %{REQUEST_FILENAME} !-d 可能也是必要的,但从您的描述中尚不清楚。


要让您的主页始终在 url 中显示“home”,您需要在索引文件中使用类似以下内容:

if ($_SERVER['REQUEST_URI'] == '/index.php' or $_SERVER['REQUEST_URI'] == '/') {
  header('Location: http://'.$_SERVER['HTTP_HOST'].'/home');
}

然后将其添加到您的 .htaccess 文件中:

RewriteRule ^home$ index.php [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^fileName1/?$ folderName/folderName/folderName/fileName1.php [L]
RewriteRule ^fileName2/?$ folderName/folderName/folderName/fileName2.php [L]

Should do what you're looking for. Assuming you want http://example.com/about.php to be seen as http://example.com/about by the users.

Adding RewriteCond %{REQUEST_FILENAME} !-d might be necessary, too, but it's not clear from your description.


To get your home page to always show "home" in the url, you'll want to use something like the following in your index file:

if ($_SERVER['REQUEST_URI'] == '/index.php' or $_SERVER['REQUEST_URI'] == '/') {
  header('Location: http://'.$_SERVER['HTTP_HOST'].'/home');
}

And then add this to your .htaccess file:

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