How to use Rewrite to include sub-directory and slug?

0

Editing: Refined the information based on the answers and doubts of the collaborators.

Internal urls in menus:
a href="/ courses / math /? slug = algebra-linear"> Math Algebra
a href="/ courses / science /? slug = molecular-structure"> Structural Sciences
a href="/ courses / english /? slug = basic-for-travel"> Basic English

I need the browser to appear:
site.com/cursos/matem/algebra-linear
site.com/cursos/ciencias/estrutura-molecular
site.com/sources/ingles/basico-para-travel

I used the Rewrite of Marcelo Rafael's edit 2 response (see below)

RewriteEngine On
RewriteBase / 
RewriteCond %{REQUEST_URI} !(\.png)|(\.css)|(\.jpg)|(\.gif)|(\.mp4)$
RewriteCond %{REQUEST_URI} !(videos/)|(duv/)$
RewriteRule "^cursos\/(.+)\/(.+)" "http://site.com/cursos/$1/?slug=$2" [R]

Results:
a url: href="/ courses / math / algebra-linear" >

Browser appears
site.com/courses/matem/?slug=algebra-linear

Is not there a reversal in Rewrite?

In both cases the linked pages open normally. And other site directories are OK.

Estrutura do site: 
.htacess
index.php
css/
js/
funções/
forum/
duvidas/
exames/
cursos/
    matem/index.php (os subdiretorios são os tipos de cursos)
        /imgs/
        /duv/
        /videos/
        pag1 <= slug
        pag2 <= slug
    ciencias/index.php
        /imgs/
        /duv/
        /videos/
        pag1 <= slug
        pag2 <= slug
    (são dezenas de cursos)

I apologize if I did not use the correct technical terms and appreciate all help and suggestions

my complete htaccess

##### LOCAWEB - NAO REMOVER #####
AddHandler php71-script .php
suPHP_ConfigPath /home/site/
##### LOCAWEB - NAO REMOVER #####

RewriteEngine On
RewriteBase / 
RewriteCond %{REQUEST_URI} !(\.png)|(\.css)|(\.jpg)|(\.gif)|(\.mp4)$
RewriteCond %{REQUEST_URI} !(videos/)|(duv/)$
RewriteRule "^cursos\/(.+)\/(.+)" "http://site.com/cursos/$1/?slug=$2" [R]

RewriteEngine On
ErrorDocument 404 /erros-4xx.php
ErrorDocument 500 /erros-4xx.php
ErrorDocument 403 /erros-4xx.php
ErrorDocument 400 /erros-4xx.php

<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
AddOutputFilterByType DEFLATE text/html
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch ^HMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
</IfModule>

<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 1 month"
ExpiresByType text/cache-manifest "access plus 0 seconds"
ExpiresByType text/html "access plus 1 hour"
ExpiresByType text/css "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType application/x-javascript "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
Header set X-UA-Compatible "IE=Edge"
<FilesMatch "\.(js|css|gif|png|jpeg|pdf|xml|oga|ogg|m4a|ogv|mp4|m4v|webm|svg|svgz|eot|ttf|otf|woff|ico|webp|appcache|manifest|htc|crx|oex|xpi|safariextz|vcf)$" >
Header unset X-UA-Compatible
</FilesMatch>
</IfModule>

I've deleted everything and just left Rewrite, but it's still the same. Where is it wrong? It's a challenge for everyone.

    
asked by anonymous 13.02.2018 / 01:37

3 answers

1

Your problem is that you are not putting the second part of the RewriteRule , ie the target, of the site you want to go, and also put this regular expression.

.htaccess

RewriteEngine On
#este é meu diretorio de testes, ex: http://localhost/stackoverflow/rewrite/
RewriteBase /stackoverflow/rewrite/   
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule "^cursos\/(.+)\/(.+)" "http://site.com/cursos/$1/?slug=$2" [R]

Here is the regular expression

link

Issue

You said that the images are not appearing, put RewriteCond to check if it is a file and if a directory.

You can see the operation of RewriteCond here: link

2 * Edition

My environment

http://localhost/stackoverflow/rewrite/
/.htaccess
/index.php
/cursos
    /assets
        /style.css
        /img.png
    /matem
        /index.php

My .htaccess

RewriteEngine On
RewriteBase /stackoverflow/rewrite/

#aqui verifica se tem .png .css e .jpg no final da requisição
#se tiver um dos, cancela o RewriteRule
RewriteCond %{REQUEST_URI} !(\.png)|(\.css)|(\.jpg)$

RewriteRule "^cursos\/(.+)\/(.+)" "http://localhost/stackoverflow/rewrite/cursos/$1/?slug=$2" [R]

My /courses/matem/index.php

<!DOCTYPE html>
<html>
<head>
    <title>Matem</title>
    <link rel="stylesheet" type="text/css" href="../assets/style.css">
</head>
<body>
    <h1>Hello</h1>
    <img src="../assets/img.png">
</body>
</html>

This is what I can do to help you, your problems with .img are vague to me, there is no way to know what exactly is happening.

    
13.02.2018 / 02:27
0

A very simple way would be:

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !/index\.php
RewriteRule ^(.*)$ index.php?req=$1&%{QUERY_STRING} [L]
Options -Indexes

index.php

<?php $_GET['req'] = isset($_GET['req']) ? str_replace( "..", "", $_GET['req'] ) : ""; ?>
    
13.02.2018 / 02:30
0

I solved it as follows:

In the .htaccess of the main index

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,NC]

And in folders (sub-directories)

In each folder I created an .htaccess with Rewrite above

And in each index.php (of folders) includes:

<base href="http://meusite.com/pasta-xx/">

It works perfectly.

    
28.02.2018 / 06:42