在htaccess文件动态重写 [英] Dynamic rewrites in htaccess file

查看:170
本文介绍了在htaccess文件动态重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一些重写和重定向在我的htaccess文件。我这样做的原因是因为我正在开发一种新的设计和布局的一个现有的网站,因此我需要重定向到留住了谷歌的排名等。

所以旧的(现有的)URL看起来是这样的:

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

和新的URL看起来是这样的:

<$p$p><$c$c>/industry-news/small-business-website-design/internet-shopper-numbers-continue-to-grow/800715788

在旧的URL 互联网购物者数量,继续对成长是动态生成的,其中作为新的URL 小型企业 - 网站设计互联网购物者数量,继续对成长 800715788 都是动态生成的。

所以我的问题是我怎么得到1网址到另一个只要动态生成的变量1是可用的。

我在想也许这将是一个好主意,做一个PHP重定向,而不​​是在那里我可以通过数据库得到的URL处理数据,以获得所需要产生新的URL中的所有数据,但我不知道这是很好的做法,这也意味着其是匹配的不是当前的情况下,旧的URL文件目录。

我猜我开始是这样的:

 选项+了FollowSymLinks
RewriteEngine叙述上
的RewriteCond /news/%{QUERY_STRING}(.)(\\d+)(.)(htm)$
重写规则^行业新闻/(.+)/% 1 /(\ d {9})
 

我知道这是很基本的,有点错了,但我在这方面的工作是第一次,我很为难。

- 额外的问题 -

我能够做这样的事?

 的RewriteCond /new/%{QUERY_STRING}(.)(\\d+).htm$
重写规则^行业新/条/ article.php?直接=%1
 

我不知道如何去真正测试这些,而是​​将这项工作?

解决方案

是的,这绝对是一个好主意,做所有重定向的东西在PHP。只要有一个简单的一揽子的.htaccess

 的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。*的index.php
 

然后变换网址,你想要在PHP并在必要时发送Location头。它可能是添加301状态code为老字号的网址是一个好主意。

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?

解决方案

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.

这篇关于在htaccess文件动态重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆