我有这样的自定义网址:
website.com/show/9999/page-name/
我试图想出一个重写规则来将其转换为:
/show/
请注意,这是一个WordPress网站,<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^show/(.*)$ /show/?id=$1 [R=301,NC,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
是WP页面名称。
这是我在.htaccess中使用的规则:
website.com/show/?id=9999
这是有效的;它会将website.com/show/9999/
重写为RewriteRule ^show/(.*)/(.*)$ /show/?id=$1&n=$2 [R=301,NC,QSA]
。
然后我修改了第二个查询字符串的规则:
website.com/show/9999/page-name/
但现在website.com/show/9999/?n=page-name
会返回404错误。如果我去:website.com/show/?id=9999&n=page-name
,它就可以了。
我做错了什么?
404问题is now solved。
但是,现在我需要重定向旧的查询字符串URL:
website.com/show/9999/page-name
到新的SEO友好网址:
private static double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == "K") {
dist = dist * 1.609344;
} else if (unit == "N") {
dist = dist * 0.8684;
}
return (dist);
}
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private static double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
如何设置重定向?
答案 0 :(得分:1)
试试这个:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^show/(\d+)/?([^/]*)/?$ /show/?id=$1&n=$2 [L,NC,QSA]
# Wordpress defaults:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
## Results
# show/9999 => show/?id=9999&n=
# show/9999/ => show/?id=9999&n=
# show/9999/page-name => show/?id=9999&n=page-name
# show/9999/page-name/ => show/?id=9999&n=page-name
如您所见,当page-name
不可用时,将会有一个空的n
查询字符串参数。在PHP脚本中,不要使用isset()
检查参数是否存在,请尝试使用empty()
。
最后一点;上述规则不会重定向请求,它会将请求映射到引擎盖下,以便URL保持干净。如果您需要重定向,只需添加R
标记即可。
即使您删除自定义重写规则,您也会从WordPress获得404。这是因为,如你所说,重写目标(/show
)是一个WordPress页面,最终会映射到index.php文件。然后,WordPress检查其数据库以查看它是否可以找到具有该路径的页面,或者如果它不能,则抛出404。显然,后者就是你的情况。
关于your recent update;要将旧URL重定向到新URL,您需要一些重写规则:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=(\d+)(&n=)?(.*)$
RewriteRule ^show/?$ /show/%1/%3 [R=301,QSD,L]
# Wordpress default rules:
# ...
</IfModule>
## Results
# show?id=9999 => show/9999
# show?id=9999&n=page-name => show/9999/page-name
# show/?id=9999&n=page-name => show/9999/page-name
不要使用[R=301]
,直到您100%确定所有内容都能正常运行,因为它会被浏览器主动缓存。
答案 1 :(得分:1)
我想我几乎已经弄明白了。
@sepehr是正确的,WordPress正在检查,没有找到页面,并抛出404.所以我需要使用WordPress自己的重写引擎来定义重写规则,以便它识别我在做什么。
所以我把它添加到我的WordPress主题的functions.php
文件中:
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^show/([^/]*)/([^/]*)/?',
'index.php?page_id=2382&id=$matches[1]&n=$matches[2]',
'top'
);
}
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'id';
$vars[] = 'n';
return $vars;
}
现在,网址website.com/show/9999/page-name
正常运行,而不是投放404。
但是,现在我需要将旧的查询字符串URL重定向到这个新的查询字符串。请参阅我的updated question。
以下是将旧查询字符串URL重定向到新的SEO友好URL的重写规则:
RewriteCond %{QUERY_STRING} ^id=([^/]*)&n=([^/]*)$
RewriteRule ^show/?$ /show\/%1\/%2\/? [R=301,L]
答案 2 :(得分:0)
请在下面的规则中查看.htaccess。
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<!-- Example based on http://bl.ocks.org/mbostock/3887118 -->
<!-- Tooltip example from http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html -->
<style>
body {
font: 11px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.tooltip {
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 1200 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
/*
* value accessor - returns the value to encode for a given data object.
* scale - maps value to a visual display encoding, such as a pixel position.
* map function - maps from data value to display value
* axis - sets up axis
*/
// setup x
var xValue = function(d) { return d.Day;}, // data -> value
xScale = d3.scale.linear().range([0, width - 200]), // value -> display
xMap = function(d) { return xScale(xValue(d));}, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
// setup y
var yValue = function(d) { return d.Hour;}, // data -> value
yScale = d3.scale.linear().range([height, 0]), // value -> display
yMap = function(d) { return yScale(yValue(d));}, // data -> display
yAxis = d3.svg.axis().scale(yScale).orient("left");
// setup fill color
var cValue = function(d) { return d.name;},
color = d3.scale.category10();
// add the graph canvas to the body of the webpage
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// add the tooltip area to the webpage
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// load data
d3.csv("11.csv", function(error, data) {
// change string (from CSV) into number format
data.forEach(function(d) {
d.Hour = +d.Hour;
d.Day = +d.Day;
// console.log(d);
});
// don't want dots overlapping axis, so add in buffer to data domain
xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]);
yScale.domain([d3.min(data, yValue)-1, d3.max(data, yValue)+1]);
// x-axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("id", "blueLine")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Hour");
// y-axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("id", "redLine")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Day");
// draw dots
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr()
.attr("r", 4)
.attr("opacity", 1)
.attr("cx", xMap)
.attr("cy", yMap)
.style("fill", function(d) { return color(cValue(d));})
.on("mouseover", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", .5);
tooltip.html(d.name + "<br/> (" + xValue(d)
+ ", " + yValue(d) + ")")
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});
// draw legend
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
// draw legend colored rectangles
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
.on("click", function(d){
// Determine if current line is visible
var active = blueLine.active ? false : true,
newOpacity = active ? 0 : 1;
// Hide or show the elements
d3.select("#blueLine").style("opacity", newOpacity);
// Update whether or not the elements are active
blueLine.active = active;
})
// draw legend text
legend.append("text")
.attr("x", width - 250)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(function(d) { return d;})
d3.selectAll("[name=v]").on("change", function() {
var selected = this.value;
display = this.checked ? "inline" : "none";
svg.selectAll(".dot")
.filter(function(d) {return selected == d.name;})
.attr("display", display);
});
d3.selectAll("[name=Day]").on("change", function(d) {
radius = this.value;
svg.selectAll(".dot")
console.log(radius);
circles.attr("r", function(d) { return d[radius]; });
});
d3.select("[name=xAX]").on("change", function(){
xAxy = this.value;
console.log(xAxy)
x.domain(d3.extent(data, function(d) { return d[xAxy]; })).nice();
svg.select(".x.axis").transition().call(xAxis);
svg.selectAll(".dot").transition().attr("cx", function(d) {
return x(d[xAxy]);
});
svg.selectAll(".x.axis").selectAll("text.label").text(axisNames[xAxy] + " (cm)");
});
d3.select("[name=yAX]").on("change", function(){
yAxy = this.value;
console.log(yAxy)
y.domain(d3.extent(data, function(d) { return d[yAxy]; })).nice();
svg.select(".y.axis").transition().call(yAxis);
svg.selectAll(".dot").transition().attr("cy", function(d) {
return y(d[yAxy]);
});
svg.selectAll(".y.axis").selectAll("text.label").text(axisNames[yAxy] + " (cm)");
});
});
</script>
<br><br>
<! Filters for the error names>
<div id="filter">
<b>Filter:</b>
<br>
<input name="v" value="Completed Users vs RedemeedCodes vs Redemeption SMS" type="checkbox" checked>Completed Users vs RedemeedCodes vs Redemeption SMS
</input>
<br>
<input name="v" value="Error_Log" type="checkbox" checked >Error_Log
</input>
<br>
<input name="v" value="CompletedUsers_AcknowledgeSMS" type="checkbox" checked >CompletedUsers_AcknowledgeSMS
</input>
<br>
<input name="v" value="OBD MissedCall Time_Lag" type="checkbox" checked>OBD MissedCall Time_Lag
</input>
<br>
<input name="v" value="Denied Permission Calls SMS Sent" type="checkbox" checked>Denied Permission Calls SMS Sent
</input>
<br>
<input name="v" value="Domex Tableau Refresh Fail" type="checkbox" checked>Domex Tableau Refresh Fail
</input>
<br>
<input name="v" value="Non IVR Entries in SMS Transactions" type="checkbox" checked>Non IVR Entries in SMS Transactions
</input>
<br>
<input name="v" value="Non Kannada Transactions" type="checkbox" checked>Non Kannada Transactions
</input>
<br>
<input name="v" value="Non OBD Entries in IVR Transactions" type="checkbox" checked>Non OBD Entries in IVR Transactions
</input>
<br>
<input name="v" value="Unique MissCallers vs Firsttime OBDs" type="checkbox" checked>Unique MissCallers vs Firsttime OBDs
</input>
<br>
<input name="v" value="UniqueCallers vs Firsttime OBDs" type="checkbox" checked>UniqueCallers vs Firsttime OBDs
</input>
<br>
<input name="v" value="User Current Position" type="checkbox" checked>User Current Position
</input>
<br>
<input name="v" value="UniqueCallers vs Firsttime OBDs
" type="checkbox" checked>UniqueCallers vs Firsttime OBDs
</input>
<br>
<input name="v" value="UniqueCallers_FirstOBDs" type="checkbox" checked>UniqueCallers_FirstOBDs
</input>
</div>
<br>
</body>
</html>