【问题标题】:Change All URLs Structure using .htaccess使用 .htaccess 更改所有 URL 结构
【发布时间】:2018-12-23 08:13:33
【问题描述】:

我在这里搜索并尝试所有解决方案,但没有一种解决方案适合我。

在我的域根目录的 Public_html 文件夹中,我安装了 WordPress。现在,在 Public_html 的目录中,我创建了一个子目录并将其命名为 app。之后我上传了一个 PHP 脚本,但我的脚本没有给我完整的路径。而且我的 URL 不会从 app 子目录加载 CSS、JS 和图像文件。

这些是我的网站 URL 链接。

Main Page
http://example.com
http://example.com/index.php

Single Video Pages
http://example.com/watch/tlb2UbRcuME
http://example.com/watch/ps2lXZegtBM
http://example.com/watch/D7cJn2Sy6FU

Search Pages
http://example.com/search/Learn+PHP
http://example.com/search/New+VEVO
http://example.com/search/New+songs

Images and Images folder Paths
http://example.com/img/logo.png
http://example.com/imgages/php.png

JavaScripts and CSS folder Paths
http://example.com/js/player.js
http://example.com/js/mini.js
http://example.com/css/style.css
http://example.com/css/player.css

Other Single Pages
http://example.com/contact
http://example.com/privacy
http://example.com/dmca

(1) 现在,我想更改目录或文件夹。

我想通过APP子目录或子文件夹访问以上所有链接。 我的意思是我想在域 URL 之后添加名为 APP 的子目录。

我想在域后的 URL 链接中添加目录或文件夹名称。 目录名是APP或者app

例如...我希望首先是所有 URL 链接和所有其他链接应该是 像这样的访问...

Main Page
http://example.com/app
http://example.com/app/index.php

Single Video Pages
http://example.com/app/watch/tlb2UbRcuME
http://example.com/app/watch/ps2lXZegtBM
http://example.com/app/watch/D7cJn2Sy6FU

Search Pages
http://example.com/app/search/Learn+PHP
http://example.com/app/search/New+VEVO
http://example.com/app/search/New+songs

Images and Images folder Paths
http://example.com/app/img/logo.png
http://example.com/app/imgages/php.png

JavaScripts and CSS folder Paths
http://example.com/app/js/player.js
http://example.com/app/js/mini.js
http://example.com/app/css/style.css
http://example.com/app/css/player.css

Other Single Pages
http://example.com/app/contact
http://example.com/app/privacy
http://example.com/app/dmca

(2) 我还想访问所有 HTTP WWW URL 使用 HTTPS 非 WWW URL。

例如:

http://www.example.com/app/dmca

进入

https://example.com/app/dmca

以上以及所有其他带有 HTTPS 非 WWW URL 的 URL。

什么 .htaccess 文件代码将用于上述情况。 有没有针对上述问题的解决方案或建议。

那是我的 .htaccess 文件。

RewriteEngine On
RewriteBase /
# RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
# RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^search/(.+)$ /search.php?q=$1
RewriteRule ^search/([^/]*?)/page/([^/]*?)/$ /search.php?q=$1&token=$2
RewriteRule ^search/([^/]*?)/$ /search.php?q=$1
RewriteRule ^download/([^/]*?)/page/([^/]*?)/$ /search.php?q=$1&token=$2
RewriteRule ^download/([^/]*?)/$ /search.php?q=$1
RewriteRule ^watch/(.+)$ /watch.php?id=$1  [L]
RewriteRule ^home/(.+)$ /index.php?q=&page=$1 [L]

RewriteRule ^(cache/|submit\.php|head\.php|head_mobile\.php|foot\.php|foot_mobile\.php|includes\.php|config\.php) - [F,L,NC]

RewriteRule ^sitemap.xml$ /sitemap.php [L]
RewriteRule ^dmca$ /dmca.php
RewriteRule ^privacy$ /privacy.php
RewriteRule ^contact$ /contact.php
ErrorDocument 404 /index.php

【问题讨论】:

  • 你能发布你尝试过的东西吗?

标签: php .htaccess


【解决方案1】:

首先你需要确保mod_rewrite.htaccess启用了槽httpd.conf然后你可以使用:

为此,http://www.example.comhttps://example.com

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

http://example.com/index.phphttp://example.com/app/index.php

RewriteRule ^$ /app [L]

这个不会在网址栏上显示/app/。如果你还想在 url 上显示/app/ 文件夹,你可以使用这个选项:

RewriteRule ^$ /app [R=301,L]

希望对你有帮助

【讨论】:

  • 您是否检查了 httpd.conf,您需要确保 mod_rewrite 和 .htaccess 已启用,否则您的主文件夹中的 .htacess 文件没有任何作用
  • mod_rewrite 和 .htacess 已启用
  • 这对我不起作用!在我的域根目录的 Public_html 文件夹中,我安装了 wordpress。现在,在 Public_html 的目录中,我创建了一个子目录并将其命名为 app 之后我上传了一个 PHP 脚本,但我的脚本没有给我完整的路径。而且我的 URL 不会从 app 子目录加载 CSS、JS 和图像文件。我想像上面一样加载所有文件。
  • 有趣的是它们都不起作用?还是只重写规则 /app ?因为第一个与子文件夹等没有关系,如果不是你有其他服务器问题,就必须工作。我还建议确保在重新加载页面时刷新浏览器缓存。
  • https 规则有效,但 /app 文件夹的重写规则无效。请检查上面我的 .htaccess 文件,我在编辑后添加到我的问题中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
相关资源
最近更新 更多