簡體   English   中英

.htaccess 301重定向+ URL重寫

[英].htaccess 301 Redirect + URL Rewrite

我得到了一個.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]

我要完成的工作是,當有人嘗試訪問舊的301重定向的url時,URL也會被重寫,因此當訪問http://example.com//someoldpage.php時,您會看到該URL http:// example。 com / fancyurl,而不是現在的樣子: http : //example.com/index.php? page=fancyurl

Redirect是mod_alias的一部分,而重寫內容是mod_rewrite的一部分。 這兩個不同的模塊都應用於相同的請求,因此導致它同時重定向和重寫。 您想要做的是,如果請求是/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]

我遇到了這個問題,並找到了解決方案,將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