简体   繁体   English

.htaccess重定向但保留网址

[英].htaccess redirect but keep url

I have the following code and I am trying to do something like that. 我有以下代码,我正在尝试做类似的事情。 Let's say I have an address - mydomain.com/server2/ when I type anything after slash for example mydomain.com/server2/whatever I want to load server2.php but address has to be the same 假设我有一个地址-mydomain.com/server2/,当我在斜杠后键入任何内容时(例如mydomain.com/server2/),无论我要加载server2.php,但地址必须相同

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
  # Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /

    # remove index.php
    RewriteRule ^index\.php/?$ / [L,R=301,NC]

    # Hide File Extensions
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^([^/]+)/$ $1.php

    # Add 301 redirects to new extensionless file
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.mydomain.com/$1 [R=301,L]

    # Add the trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ http://www.mydomain.com/$1/ [R=301,L]


</IfModule>

also I don't know if above code is 100% correct. 我也不知道上面的代码是否100%正确。 Thanks for any help. 谢谢你的帮助。

There are problems in your code and in certain URLs it will give you infinite looping. 您的代码中有问题,某些URL中会出现无限循环。 Replace your code with this: 用以下代码替换代码:

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /

    # remove index.php
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^ /%1/ [R=301,L,NE]

    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ /$1.php [L]

    # Add the trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

</IfModule>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM