Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
769 views
in Technique[技术] by (71.8m points)

php - htaccess 301 redirect for URL with parameter

I have the following in my htaccess file, which takes a URL like: www.example.com/Page.php?id=About and turns it into www.example.com/about. The only issue is that the old URLs are not redirecting to the new URLs when I set up Redirect 301s in the htaccess (which I understand is because you're not supposed to pass parameters in basic Redirect 301s - example of basic Redirect that I've tried below).

Redirect 301 /Page.php?id=About http://www.example.com/about

The catch is that the new URLs don't necessarily just strip out the current id and replace it with a lowercase version. For example, I would also want to:

Redirect 301 /Page.php?id=Example http://www.example.com/example-page

I want to keep the current functionality of rewriting the URLs, but also want any visits to the old URLs to redirect to the new ones (i.e. to update Google index). Visiting www.example.com/Page.php?id=About at the moment still works but shows a page with no content.

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9-]+)/?$ Page.php?id=$1
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ Page.php?id=$1&tab=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ Page.php?id=$1&tab=$2&tabid=$3
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you add this rule to your .htaccess it should do the work of the Redirect. It will add the ?id=About to the redirect to /about. If you are running on version 2.4.0 or later, you can add the QSD flag to discard of the query string in the redirect.

RewriteCond %{QUERY_STRING} ^id=About$
RewriteRule ^page.php /about [NC,R,L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...