我想创建.htaccess mod重写,但仍然有问题。
假设我创建了这样的友好 URL:
mydomain.com/mypage/12345/title-of-news
我希望该友好的URL在隐藏进程中处理以下URL:
mydomain.com/index.php?p=mypage&idnews=12345
"p"和"idnews"的值是动态的,因此它们将具有不同的值。
我尝试了下面的代码,但仍然不起作用。有什么不对吗?
RewriteBase /
RewriteCond %{QUERY_STRING} p=('w+)&idnews=('d+)
RewriteRule ^index.php /%1/%2 [L,R=301]
任何帮助将不胜感激。抱歉,如果这是重复的问题,如果不介意,请告诉我答案链接。多谢。
我认为你不需要你在那里写的RewriteCons。
您可以使用以下规则
RewriteRule ^/?([0-9a-zA-Z_.-]+)/([0-9]+)/([0-9a-zA-Z_.-]+)$ index.php?p=$1&idnews=$2 [L]
如果要将index.php?p=foo&idnews=bar
更改为/foo/bar
,请尝试以下规则:
RewriteEngine on
##externally redirect "/index.php?p=foo&idnews=bar" to "/foo/bar"##
RewriteCond %{THE_REQUEST} /index'.php'?p=([^&]+)&idnews=([^&'s]+) [NC]
RewriteRule ^ /%1/%2? [L,R,NC]
##internally redirect "/foo/bar" to "/index.php?p=foo&idnews=bar"##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?(.*)?$ /index.php?p=$1&idnews=$2 [NC,L]