C
C
Cider Cherenkov2018-11-21 02:58:18
htaccess
Cider Cherenkov, 2018-11-21 02:58:18

How to make correct htaccess?

Hello. There is a news site written in pure php. I ran into a problem in htacces, in more detail:
The site has news categories, subcategories of the 2nd level and, accordingly, the news itself. News always lies only in the category of the 2nd level.
In htaccess, it is written so that the url address is built in this way: http://site.ru/catname/catname_2/id-news/where, as I think, is clear (catname is the main category, catname_2 is a subcategory, id is the id of the news (so that there are no duplicates in the url), news is the url address itself (header alias)) .
htaccess is written like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)(?<!/)$ /$1/ [R=301,L]

#
... 301 редиректы и т.д.
#

RewriteRule ^([a-zA-Z0-9_-]+)/?$ news.php?category=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/(page[0-9]*)?/?$ news.php?category=$1&page=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ news.php?category=$1&subcategory=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(page[0-9]*)?/?$ news.php?category=$1&subcategory=$2&page=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]*)-([a-zA-Z0-9_-]+)/?$ news.php?category=$1&subcategory=$2&id=$3-&url=$4 [L]

Problem:
With such a structure, you can type anything in the url, for example http://site.ru/782374/, and it will consider it as a news category ... (it’s not a problem to write conditions in news.php, but it’s all wrong)
For example, the redirect to 404 page does not work etc.
Add a prefix in the form
http://site.ru/news/category/sub.................../
- I don’t want it at all, I want just such a structure

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-11-21
@Stalker_RED

There are two options:
1. register all categories explicitly in htaccess
2. make normal routing in php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question