简体   繁体   English

重定向并将 url 从 html 更改为无 html htaccess

[英]redirect and change url from html to none html htaccess

I need to redirect domain.com/abcdef.html to domain.com/abcdef and the new url´s content should be domain.com/abcdef.html.我需要将 domain.com/abcdef.html 重定向到 domain.com/abcdef 并且新 url 的内容应该是 domain.com/abcdef.html。

I realy struggle here.我真的在这里挣扎。 I can achieve to get the content from the html shown without having the html in the url, but then both urls are available.我可以实现从显示的 html 中获取内容,而无需在 url 中包含 html,但是两个 url 都可用。

When I try to achieve both I get an endlessloop and an ERR 500, but I have no clue how I can prevent this endlessloop.当我尝试同时实现时,我得到了一个无限循环和一个 ERR 500,但我不知道如何防止这个无限循环。

so my try one - making both urls available with the same content looks like this:所以我的尝试 - 使两个网址都具有相同的内容看起来像这样:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
RewriteRule ^([^\.]+)$ $1.html [NC,L]

The second rule is just the same withtout the first two lines:第二条规则与前两行相同:

RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
RewriteRule ^([^\.]+)$ $1.html [NC,L]

resulting into an endless loop.导致无限循环。 My research only brought me to solutions for either one or for a try with the index.php.我的研究只是让我找到了解决方案,或者尝试使用 index.php。 But I search for a htaccess-only solution.但我在寻找一个 htaccess-only 解决方案。

Is it possible?是否可以?

EDIT: I understand why the endlessloop happens I request blabla.html --> rule 1 makes it to blabla and requests again, rule 2 makes it to blabla.html requests again and so on.编辑:我明白为什么会发生无限循环我请求 blabla.html --> 规则 1 使它成为 blabla 并再次请求,规则 2 使它再次成为 blabla.html 请求,依此类推。 But how can I prevent this?但是我怎样才能防止这种情况呢?

EDIT 2: So I tried as suggested by starkeen, and had this:编辑 2:所以我按照 starkeen 的建议进行了尝试,结果如下:

RewriteEngine On
RewriteCond %{ENV_REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteBase /
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
RewriteRule ^([^\.]+)$ $1.html [NC,L]

which still lead to the endlessloop.这仍然导致无限循环。 Using it with the RewriteCond still has both available (blabla.com/content.html and blabla.com/content)将它与 RewriteCond 一起使用仍然可用(blabla.com/content.html 和 blabla.com/content)

Unfortunately it is not apache 2.4 so I couldnt try the END tag, but I try to reach my hoster and ask them if I they update it.不幸的是,它不是 apache 2.4,所以我无法尝试使用 END 标签,但我尝试联系我的主机并询问他们是否更新了它。

Or did I get you wrong and mix something up with the htaccess?还是我误会了您将某些东西与 htaccess 混淆了?

EDIT 3: So since my code should work, but doesnt, here the whole htaccess to make sure there isn´t anything else that blocks the **** from working.编辑 3:因此,由于我的代码应该可以工作,但不能,这里是整个 htaccess 以确保没有其他任何东西阻止 **** 工作。

AuthName "nottellingyou" 
AuthType Basic 
AuthUserFile /home/www/top/secret/.htpasswd 
AuthGroupFile /dev/null 
require valid-user

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV_REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteBase /
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

To prevent the loop error, put the following rule bellow RewriteEngine On line为了防止循环错误,把下面的规则放在下面RewriteEngine On line

RewriteCond %{ENV_REDIRECT_STATUS} 200
RewriteRule ^ - [L]

This terminates internal rewrite processing on 2nd iteration so /file.html will not get rewritten to the same path again.这将终止第二次迭代的内部重写处理,因此 /file.html 不会再次被重写到相同的路径。

On apach 2.4 you can use END flag in your RewriteRule to prevent the internal loop在 apach 2.4 上,您可以在 RewriteRule 中使用 END 标志来防止内部循环

RewriteRule ^([^.]+) $1.html [END]

To remove the .html extension, you can also use this script :要删除 .html 扩展名,您还可以使用此脚本:

RewriteEngine on


#1)redirect /file.html to /file

RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NE,L,R]
 #2)check if /file exists as an .html ,if it exists ,rewrite /file to /file.html
RewriteCond %{REQUEST_FILENAME}.html -f

RewriteRule ^(.*?)/?$ /$1.html [L]

If you have a website with URLs like magepow.com/hello-world.html 1 and if you want to redirect that to magepow.com/hello-world/, then you can try the below code.如果您的网站的 URL 为 magepow.com/hello-world.html 1,并且您想将其重定向到 magepow.com/hello-world/,那么您可以尝试以下代码。 Copy/paste this into your .htaccess file.将其复制/粘贴到您的 .htaccess 文件中。 Make sure you replace magepow.com 1 with your own URL.确保将 magepow.com 1 替换为您自己的 URL。

RedirectMatch 301 ^/([^/]+).html$ http://magepow.com/$1

This should work fine, and if your website has 'HTTPS' instead of 'HTTP', do change that as well.这应该可以正常工作,如果您的网站使用“HTTPS”而不是“HTTP”,也请进行更改。

Note: If the above code is not working for your website, you can try these instead:注意:如果上述代码不适用于您的网站,您可以尝试以下代码:

RedirectMatch 301 ^/([^/]+)/([^/.]+).html$ /$1/$2/
RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/.]+).html$ /$1/$2/$3/
RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/.]+).html$ /$1/$2/$3/$4/

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

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