【问题标题】:How do I change my URL to a clean URL with directories?如何将我的 URL 更改为带有目录的干净 URL?
【发布时间】:2021-09-30 15:18:27
【问题描述】:

我希望我的网站(像大多数网站一样)有干净的 URL,以便每个页面都采用目录格式。

目前,我开发了自己的网站,因此如果我访问 site.com/mypage,它会将其视为 site.com/?p=mypage。我使用以下脚本通过 .htaccess 执行此操作:

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+/mysite/index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/mysite/([^/]+)/?$ [NC]
RewriteRule ^ index.php?page=%1 [L,QSA]

这很好用。现在我想使用目录样式格式,这样我就可以做 site.com/mypage/myinfo。我尝试了几种不同的方法。

首先,我只是尝试在“p”变量中提供目录,这样如果我提供了 site.com/?p=mypage%2Fmyinfo,结果将是 site.com/mypage/myinfo。这可行,但它会尝试在不存在的目录中查找页面。所以我的网站坏了。

其次,我想我可以将它们都作为单独的变量,例如 site.com/?p=mypage&c=myinfo。这可能有效,但我正在努力重写 .htaccess 以便它同时接受变量并正确放置它们。

我欢迎任何帮助,帮助我重写此 .htaccess 文件以适应两者,或者为我需要的另一种方法提供建议。

谢谢!

【问题讨论】:

  • 如果我的回答对您有帮助,请告诉我,谢谢。

标签: .htaccess url mod-rewrite


【解决方案1】:

用你展示的样本,尝试;请尝试遵循 htaccess 规则。请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine On
##New rule for uri mypage/myinfo to be served by index.php internally.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(mypage)/(myinfo)/?$ index.php?p=$1&c=$2 [NC,L]

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+/mysite/index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/mysite/([^/]+)/?$ [NC]
RewriteRule ^ index.php?page=%1 [L,QSA]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 2014-09-26
    • 2011-04-02
    • 1970-01-01
    相关资源
    最近更新 更多