Simple mod rewrite query string but still failed

旧时模样 提交于 2020-01-02 19:23:22

问题


I want to create .htaccess mod rewrite but still have problem.

Suppose i created friendly URL like this :

mydomain.com/mypage/12345/title-of-news

I want that friendly URL process the below URL in hidden process :

mydomain.com/index.php?p=mypage&idnews=12345

Values of "p" and "idnews" are dynamic, so they will have different value.

I tried below code but still didn't work. Anything wrong?

RewriteBase /
RewriteCond %{QUERY_STRING} p=(\w+)&idnews=(\d+)
RewriteRule ^index.php /%1/%2 [L,R=301]

Any help would be appreciated. Sorry if this is duplicated question, if don't mind please tell me the answer link. Thanks a lot.


回答1:


If you want to change index.php?p=foo&idnews=bar to /foo/bar , try theses rules :

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]



回答2:


I think you don't need the RewriteCond you write there.

And you can use following rule

RewriteRule ^/?([0-9a-zA-Z_.-]+)/([0-9]+)/([0-9a-zA-Z_.-]+)$ index.php?p=$1&idnews=$2 [L]


来源:https://stackoverflow.com/questions/36109639/simple-mod-rewrite-query-string-but-still-failed

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