.htaccess在地址栏中显示指向实际服务器文件夹路径的较短url

.htaccess在地址栏中显示指向实际服务器文件夹路径的较短url,.htaccess,.htaccess,我试图缩短地址栏中的URL,并用于谷歌索引。例如,实际的服务器目录路径 将显示 我发现了许多类似的话题,但没有一个答案似乎适用于这个特殊的案例 例如,我有: RewriteRule ^path3(.*)$ path1/path2/path3$1 (tried with with [L], [QSA,l], [R=301,...]...) 但这只是重定向,不会在浏览器中保留短地址 我的.htaccess文件如下所示: Options +FollowSymLinks RewriteEngine

我试图缩短地址栏中的URL,并用于谷歌索引。例如,实际的服务器目录路径 将显示

我发现了许多类似的话题,但没有一个答案似乎适用于这个特殊的案例

例如,我有:

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

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

RewriteEngine On
RewriteRule ^test2.txt/?$ /test.txt
这让我有一个可选的尾部斜杠,并在/test2.txt上显示test.txt的内容


假设您的重写引擎已打开,您是否可以复制此行为?您使用的是什么版本的Apache?路径是否由CMS处理?

您提供的示例在我自己的测试网站上适用,使用以下方法。htaccess rewrite:

RewriteEngine On
RewriteRule ^test2.txt/?$ /test.txt
这让我有一个可选的尾部斜杠,并在/test2.txt上显示test.txt的内容


假设您的重写引擎已打开,您是否可以复制此行为?您使用的是什么版本的Apache?路径是否由CMS处理?

将我的评论转换为答案,以便可以轻松找到所述问题的解决方案

所示规则存在两个问题:

  • 由于目标路径指向现有目录,并且目标中没有尾随的
    /
    ,因此Apache的
    mod_dir
    模块会导致外部重定向,该模块会在目录路径的末尾附加一个
    /
    ,并执行
    301
    重定向
  • 规则的位置不正确
  • 不重要,但重定向规则中缺少
    L
    NE
    (无转义)标志,这在某些情况下可能会导致问题
  • 根据这些建议,最终工作
    .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]
    

    将我的评论转换为答案,以便能够轻松找到所述问题的解决方案

    所示规则存在两个问题:

  • 由于目标路径指向现有目录,并且目标中没有尾随的
    /
    ,因此Apache的
    mod_dir
    模块会导致外部重定向,该模块会在目录路径的末尾附加一个
    /
    ,并执行
    301
    重定向
  • 规则的位置不正确
  • 不重要,但重定向规则中缺少
    L
    NE
    (无转义)标志,这在某些情况下可能会导致问题
  • 根据这些建议,最终工作
    .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]
    

    嗨,谢谢你的回复。Apache 2.4.7取消绑定。路径不是由CMS创建的,它指向一个实际的服务器文件。您的地址栏保留了/test2.txt?我使用的是Apache v2.4.6,我已经获取了您的整个.htaccess,并复制了
    path1
    path2
    ,最后文件是
    path3
    path3test
    (用于测试
    $1
    添加),这两个URL掩码都按照当前.htaccess中提供的方式工作。path3是文件还是其他目录?谢谢。帮助我做测试很有帮助。嗨,谢谢你的回复。Apache 2.4.7取消绑定。路径不是由CMS创建的,它指向一个实际的服务器文件。您的地址栏保留了/test2.txt?我使用的是Apache v2.4.6,我已经获取了您的整个.htaccess,并复制了
    path1
    path2
    ,最后文件是
    path3
    path3test
    (用于测试
    $1
    添加),这两个URL掩码都按照当前.htaccess中提供的方式工作。path3是文件还是其他目录?谢谢。帮助我做测试。当然,给你。这是一个内部重定向。我更改了强制www.rule(见上文),清除了缓存,仍然有相同的结果。是的,这是一个真实的物理路径。将此规则移到上一个301规则下方。另外,请告诉您使用什么URL来测试此规则谢谢!这是一个秩序问题。通过在文件中向上移动条件解决了问题。当然,给你。这是一个内部重定向。我更改了强制www.rule(见上文),清除了缓存,仍然有相同的结果。是的,这是一个真实的物理路径。将此规则移到上一个301规则下方。另外,请告诉您使用什么URL来测试此规则谢谢!这是一个秩序问题。通过在文件中向上移动条件,解决了问题。