Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

mod rewrite - .htaccess friendly URl

Can anybody please help me with some URL rewriting?

I had: (EXAMPLES)

www.example.com/index.php?page=namepage
www.example.com/index.php?page=gallery&topic=nametopic
www.example.com/index.php?page=homepage&paging=1

I would like to have:

www.example.com/namepage
www.example.com/gallery/nametopic
www.example.com/homepage/1

I have in my htaccess file:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ?page=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)?/?$ ?page=$1&topic=$2

But it doesnt work very well, because i can write:

  • www.example.com/index.php?page=namepage (page or whatever)
  • www.example.com/?page=namepage (page or whatever)
  • www.example.com/namepage/
  • www.example.com/namepage (THIS I WANT - no others)

And second problem is:

  • www.example.com/namepage (OK, i want, we see namepage)
  • www.example.com/namepage/whatever (NO OK, i want 404, but we see namepage)
  • www.example.com/gallery/topic (OK, i want, we see nametopic)
  • www.example.com/whatever/whatever2/whatever3 (OK, i want 404)

VERY THANKS TO ANYBODY.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
### all your redirects

# for www.example.com/index.php?page=homepage&paging=1
RewriteCond %{THE_REQUEST} ?page=([^&]+)&paging=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]

# for www.example.com/index.php?page=gallery&topic=nametopic
RewriteCond %{THE_REQUEST} ?page=([^&]+)&topic=([^& ]+)
RewriteRule ^ /%1/%2? [L,R=301]

# for www.example.com/index.php?page=namepage
RewriteCond %{THE_REQUEST} ?page=([^& ]+)($| )
RewriteRule ^ /%1? [L,R=301]

# for www.example.com/namepage/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /$1 [L,R=301]

### all your rewrites back

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&paging=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&topic=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...