简体   繁体   English

.htaccess 301重定向+ URL重写

[英].htaccess 301 Redirect + URL Rewrite

I got a .htaccess that pretty much looks like this: 我得到了一个.htaccess文件,它看起来像这样:

# disable directory browsing
Options All -Indexes

# start rewrites
RewriteEngine on

Redirect 301 /someoldpage.php http://example.com/fancyurl

# if not a file, and not a directory, reroute through index as normal page
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

What I want to accomplish is that the url is rewritten also when someone is trying to access an old 301 redirectred url, so when going to http://example.com//someoldpage.php you see the url http://example.com/fancyurl and NOT like now: http://example.com/index.php?page=fancyurl 我要完成的工作是,当有人尝试访问旧的301重定向的url时,URL也会被重写,因此当访问http://example.com//someoldpage.php时,您会看到该URL http:// example。 com / fancyurl,而不是现在的样子: http : //example.com/index.php? page=fancyurl

Redirect is part of mod_alias, while the rewrite stuff is part of mod_rewrite. Redirect是mod_alias的一部分,而重写内容是mod_rewrite的一部分。 The two different modules both get applied to the same request and thus causing it to redirect and rewrite at the same time. 这两个不同的模块都应用于相同的请求,因此导致它同时重定向和重写。 What you want to do is have it redirect if the request is /someoldpage.pjp and not rewrite to index.php, so you need to use only mod_rewrite here: 您想要做的是,如果请求是/someoldpage.pjp ,则将其重定向,而不是重写为index.php,因此您只需要在此处使用mod_rewrite:

# disable directory browsing
Options All -Indexes

# start rewrites
RewriteEngine on

RewriteRule ^someoldpage\.php$ http://example.com/fancyurl [L,R=301]

# if not a file, and not a directory, reroute through index as normal page
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

I Struck with this problem and got a solution in my case, to rewrite the url from example.com/about.php to example.com/about with the following code in your htaccess. 我遇到了这个问题,并找到了解决方案,将htaccess中的以下代码从example.com/about.php重写为example.com/about

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]

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

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