开发者

301 redirect file to clean url

开发者 https://www.devze.com 2023-03-14 01:32 出处:网络
Working on redirects for my page. I have a list of all the old and new urls. The old urls are /directory/subdirectory/file.php

Working on redirects for my page. I have a list of all the old and new urls.

The old urls are /directory/subdirectory/file.php

The new urls are clean urls

/directory/subdirectory/file/

Some new urls match the old urls without the ".php" while some other do not.

One webpage suggested:

RedirectMatch 301 ^/sales/products/item$ /products/item-name/

But would the following give me a redirect loop?

RedirectMatch 301 ^/privacy-policy$ /privacy-policy/

I haven't tried it yet开发者_运维技巧, but hoping to not get myself in trouble.


Using Apache's mod_rewrite -- add these lines into your .htaccess file:

# Activate Rewrite Engine
RewriteEngine On

# redirect *.php to "nice" url
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.php$ $1/ [R=301,QSA]

# process "nice" urls by looking for appropriate .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L,QSA]

These rules will do 2 things:

1) redirect (301) *.php to "nice" urls: directory/subdirectory/file.php will become directory/subdirectory/file/

2) properly serves such "nice" urls, e.g. directory/subdirectory/file/ will be treated as directory/subdirectory/file.php.

P.S. If redirect does not work (may happen sometimes depending on configuration) try adding slash before $1 (e.g. /$1 )

0

精彩评论

暂无评论...
验证码 换一张
取 消