P
P
plac2018-03-14 14:54:03
htaccess
plac, 2018-03-14 14:54:03

Beautiful URL problem (htaccess), how to fix?

Hello, I tried to make a CNC (beautiful url) for a self-written site and there was one problem that I can’t solve. I've searched half the internet and nothing came up.
I implement CNC through htaccess, now all its code looks like this:

RewriteEngine on
RewriteRule ^module_news/([0-9]+)$ module_news/fullnews_page.php?id=$1 [QSA,NC]

As you can see, I have dynamic pages with GET requests and there were no problems here - the /module_news/fullnews_page.php?id=17 page is loaded via the /module_news/17 address.
BUT!
First, we get duplicates - the same page is available at 2 URLs.
Secondly, it is necessary to somehow change the address, that is, if the user enters /module_news/fullnews_page.php?id=17, it redirects (replaces it in the URL line) with a beautiful /module_news/17 . That is, a beautiful URL was a priority.
Options with third-party frameworks are not the same.
Please describe your solution in as much detail as possible. And in principle, what is the best way to make beautiful URLs?
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
evilray, 2018-03-14
@evilray

It’s more correct to set up the modrewright so that it sends any request to index.php, and already parse the string in it as you like. More about the front controller . An example in php .
Parsing the url string and matching the address with the desired module (controller / action) in the front controller is called routing.
Redirects:
You can search for more details, for example, on the request " htaccess redirect ".

V
Viktor Taran, 2018-03-14
@shambler81

1. The engines of these urls can have up to 5, all that can be done is 301
2.

RewriteCond %{QUERY_STRING} (^|&)id\=([0-9]{1,})\$($|&)
RewriteRule ^.+[^/]/fullnews_page\.php$ /$1/%2? [L,R=301]

The get is not part of the url and we select it differently
[0-9]{1,}- from zero to 9, one or more times (this is the second bracket)
.+[^/]more than 1 character but not a slash, before the slash (well, a crutch, you can also use quantifiers)
/$1/%2the first bracket from the wheel and the second from the conda
?- at the end, remove the get parameter, leave a beautiful cnc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question