Harris
Harris

Reputation: 87

Simple mod rewrite query string but still failed

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.

Upvotes: 2

Views: 95

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

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]

Upvotes: 0

Zakir Hossain
Zakir Hossain

Reputation: 108

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]

Upvotes: 1

Related Questions