简体   繁体   中英

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 (ie 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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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