How to configure advanced options in mod_rewrite

I am in a new project and I am designing a URL structure, I want the URLs to look like this: /category-23/keyword/5/ If a normal page: /search.php?q=keyword&cat=23&page=5 So my question is: the cat and page fields should be optional, I mean, if I go to /keyword, this it should be /search.php?q=keyword(page 1) and if I go it /category/keywordshould be: /search.php?q=keyword&cat=category&p=1 and also if I go /keyword/5/it should be:/search.php?q=keyword&p=5

Now I have my .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ search.php?q=$2&cat=$1&page=$3 [L]

I can't get it to work, and CSS / image files are not loading. I would thank many who could give me a solution.

+3
source share
3 answers

, :

RewriteRule ^([^/]+)$ search.php?q=$1
RewriteRule ^([^/]+)/([0-9]+)$ search.php?q=$2&p=$1
RewriteRule ^([^/]+)/([^/]+)$ search.php?q=$2&cat=$1&p=1
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)$ search.php?q=$2&cat=$1&p=$3

, , :

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ - [L]

, , , , , URL, css/style.css ./css/style.css. URL-, URL- URL- , . /category/keyword , css/style.css, /category/keyword/css/style.css /css/style.css. URL /css/style.css URL-

+6

, @Gumbo , ... / , APP_PATH (php) vars ... href [href]

...

<base href="http://<?php echo $_SERVER[HTTP_HOST];?><?php echo APP_PATH;?>"/>

, ... @Gumbo , ... -e-o on:).

+1

Must not be

RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [L]

@Gumbo's answer above? It works with me like that. If it is a file (e.g. CSS), skip it.

+1
source

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


All Articles