Dynamic rewrites in htaccess file

ε祈祈猫儿з 提交于 2019-12-20 06:03:34

问题


I am working on some rewrites and redirects in my htaccess file. My reason for doing this is because I am developing a new design and layout for an existing website therefore I need to redirect to keep hold of the Google rankings etc.

So the old (existing) URL looks like this:

/news/internet-shopper-numbers-continue-to-grow-$21378232.htm

and the new URL will look like this:

/industry-news/small-business-website-design/internet-shopper-numbers-continue-to-grow/800715788

In the old URL internet-shopper-numbers-continue-to-grow is dynamically generated, where as in the new URL small-business-website-design, internet-shopper-numbers-continue-to-grow, and 800715788 are all dynamically generated.

So my issue is how do I get from 1 URL to another if only 1 of the dynamically generated variables is available.

I was thinking perhaps it would be a good idea to do a PHP redirect instead where I can get the data from the URL process through database to get all data needed to generate the new URL, but I'm not sure if this is good practice, this would also mean having to match the old URL file directory which is not the current case.

I'm guessing I start with something like this:

Options +FollowSymlinks
RewriteEngine On
RewriteCond /news/%{QUERY_STRING}(.)(\\d+)(.)(htm)$
RewriteRule ^industry-news/(.+)/%1/(\d{9})

I know this is very basic and kinda wrong but I'm working on this for the first time and I'm stumped.

-- extra question --

am I able to do something like this?

RewriteCond /new/%{QUERY_STRING}(.)(\\d+).htm$
RewriteRule ^industry-new/article/article.php?direct=%1

I dont know how to go about testing these really, but would that work?


回答1:


Yes, it's definitely a good idea to do all redirection stuff in php. Just have a simple "catch-all" .htaccess

RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  .* index.php

then transform urls as you want in php and send a location header when necessary. It might be a good idea to add 301 status code for "old" urls.



来源:https://stackoverflow.com/questions/7281778/dynamic-rewrites-in-htaccess-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!