.htaccess friendly url

Can someone help me with some rewriting url?

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:

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

I have a 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 does not work very well, because I can write:

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

And the second problem:

  • www.example.com/namepage (OK, I want, we see the name of the page)
  • www.example.com/namepage/whatever (NO, I want 404, but we see the page name)
  • www.example.com/gallery/topic (OK, I want, we see a tampetop)
  • www.example.com/whatever/whatever2/whatever3 (OK, I want 404)

VERY THANKS TO ANYONE.

+4
source share
1 answer
 ### 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] 
+7
source

Source: https://habr.com/ru/post/1435587/


All Articles