【问题标题】:.htaccess Display shorter url in address bar that points to a real server folder path.htaccess 在指向真实服务器文件夹路径的地址栏中显示较短的 url
【发布时间】:2020-03-18 18:32:45
【问题描述】:

我正在尝试缩短地址栏中的网址并用于 Google 索引。比如一个真实的服务器目录路径 http://www.somewebsite.com/path1/path2/path3/ 会显示 http://www.somewebsite.com/path3/

我发现了许多类似的主题,但没有似乎适用于这种特殊情况的答案。

例如:

RewriteRule ^path3(.*)$ path1/path2/path3$1 (tried with with [L], [QSA,l], [R=301,...]...)

但这只是进行重定向,不会在浏览器中保留短地址。

我的 .htaccess 文件如下所示:

Options +FollowSymLinks

RewriteEngine On
RewriteBase /

# Use UTF-8 encoding for anything served as `text/html` or `text/plain`.
AddCharset UTF-8 .html

# Force www.
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)/index$ $1/ [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

#Trying to have a shorter url in address bar
RewriteRule ^path3(.*)$ path1/path2/path3$1

【问题讨论】:

  • 好的,给你。
  • 这是一个内部重定向。我改变了力量 www。规则(见上文),清除缓存仍然有相同的结果。
  • 是的,是真实的物理路径。
  • 将此规则上移到最后的 301 规则之下。还要告诉你使用什么 URL 来测试这个规则
  • 谢谢!这是一个秩序问题。通过在文件中上移条件解决了问题。

标签: .htaccess


【解决方案1】:

将我的 cmets 转换为 answer,以便可以轻松找到所述问题的解决方案。

问题中显示的规则有几个问题:

  1. 由于目标路径指向现有目录,并且目标中没有尾随 /,它会导致 Apache 的 mod_dir 模块进行外部重定向,该模块在目录路径末尾附加 / 和执行301 重定向。
  2. 规则定位不正确。
  3. 不重要,但在重定向规则中缺少 LNE(无转义)标志,在某些情况下可能会导致问题。

有了这些建议,最终工作 .htaccess 可以是这样的:

# Use UTF-8 encoding for anything served as `text/html` or `text/plain`.
AddCharset UTF-8 .html
Options +FollowSymLinks

RewriteEngine On
RewriteBase /    

# Force www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# remove .php and index; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.php[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/$ $1 [R=301,L,NE]

# rewrite path3/ to /path1/path2/path3/ 
RewriteRule ^path3/.*$ path1/path2/$0 [L,NC]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*?)/?$ $1.php [L]

【讨论】:

    【解决方案2】:

    您提供的示例适用于我自己的测试网站,使用以下 .htaccess 重写:

    RewriteEngine On
    RewriteRule ^test2.txt/?$ /test.txt
    

    这让我有一个可选的尾部斜杠,并在 /test2.txt 上显示 test.txt 的内容。

    假设你的重写引擎是开启的,你能复制这个行为吗?您使用的是什么版本的 Apache?路径是否完全由 CMS 处理?

    【讨论】:

    • 您好,感谢您的回复。 Apache 2.4.7 Unbuntu。该路径不是由 CMS 创建的,它指向一个实际的服务器文件。您的地址栏保留/test2.txt?
    • 我使用的是 Apache v2.4.6,我已经获取了您的整个 .htaccess 并复制了 path1path2,最终文件为 path3path3test (测试$1 添加),并且两个 URL 掩码都按照您当前的 .htaccess 中提供的方式工作。 path3 是文件还是其他目录?
    • 谢谢。对帮助我进行测试很有帮助。
    猜你喜欢
    • 2011-09-11
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    相关资源
    最近更新 更多