【发布时间】: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