重定向301 - 从URL中删除.html扩展名

时间:2015-12-24 23:50:51

标签: regex .htaccess redirect

我想从我的网址中删除#include <iostream> #include <ostream> #include <sstream> #include <mutex> class Logger { public: // destructor is called when output is collected and // the temporary Logger() dies (after the ";"). ~Logger() { Logger::mutex_.lock(); // make sure I am the only one writing std::cout << buffer_.str(); // write std::cout << std::flush; // push to output Logger::mutex_.unlock(); // I am done, now other threads can write } // funny construct you need to chain logs with "<<" like // in std::cout << "var = " << var; template <typename T> Logger &operator<<(T input) { buffer_ << input; return *this; } private: // collect the output from chained << calls std::ostringstream buffer_; // mutex shared between all instances of Logger static std::mutex mutex_; }; // define the static variable. This line must be in one .cpp file std::mutex Logger::mutex_; int main() { Logger() << "Hello" << " " << "World\n"; Logger() << 123 << "test\n"; return 0; } 扩展名,位于特定目录并重定向301。

以下是结构的样子:

.html

问题是mysite.com/category/nameofcategory/pagenumber.html nameofcategory可以是任何字母或数字。

你能帮帮我吗?

2 个答案:

答案 0 :(得分:0)

要删除网址上的.html扩展名并将301重定向到无扩展名网址,您可以在&#34;特定目录中的.htaccess中尝试以下操作&#34;:

RewriteEngine On
RewriteBase /specific-directory
RewriteRule ^(.*)\.html$ $1 [R=301,L]

答案 1 :(得分:0)

我不建议将您的内容分散在不同文件夹中的许多html文件中。如果您想要更改页面的布局,这将变得非常不切实际。

将内容存储在数据库中是一种更好的解决方案。如果这不可能,html文件可能只包含格式化的文本内容,后端脚本可以在请求页面时将该内容嵌入到布局中。

这要求在Apache配置中启用mod_rewrite模块。

在这两种情况下,所有请求都将通过后端脚本进行路由,而.htaccess可能如下所示:

RewriteEngine on
RewriteRule ^category/([^/.]+)/([^/.]+)/?$ index.php?category=$1&page=$2 [L]

这部分正则表达式:([^/.]+)匹配并捕获不包含字符/或的字符串。并且是1个字符长或更长。捕获的字符串可以用$ 1,$ 2等引用。

现在像mysite.com/category/foo/bar这样漂亮的网址可以正常工作。此外,我们需要定义一个规则,重定向以“.html”结尾的旧网址。所需的规则可能如下所示:

RewriteRule ^category/([^/.]+)/([^/.]+).html$ category/$1/$2 [R=301,L]

在测试和调整重定向时要记住的一件事是重定向可能会在浏览器中缓存,这可能会在测试时导致混乱的结果。