How to use Rewrite to include subdirectory and Slug?

Asked

Viewed 585 times

0

Edition: I refined the information based on the answers and questions of the employees.

internal urls in menus:
a href="/cursos/matem/? Slug=Algebra-linear">Mathematics Algebra
a href="/cursos/ciencias/? Slug=structure-molecular">Sciences Structures
a href="/courses/ingles/? Slug=basico-para-viagens">Ingles Básico

I need it to appear in the browser:
site.com/cursos/matem/Algebra-linear
site.com/courses/science/molecular structure
site.com/courses/English/basic-for-travel

I used the Rewrite of Marcelo Rafael’s Reply Issue 2 (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:
the url: href="/courses/matem/Algebra-linear">

In the browser appears
site.com/cursos/matem/? Slug=Algebra-linear

There is no inversion in Rewrite?

In both cases the linked pages open normally. And other directories of the site 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 I appreciate all help and suggestion

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 already erased everything and only left Rewrite, but it’s still the same.
Where is it wrong? It’s a challenge for everyone.

3 answers

1

Your problem is that Voce is not putting the second part of RewriteRule that is, the target, the site Voce wants 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

https://regex101.com/r/DcYGkZ/1

Editing

You said the images aren’t showing, put RewriteCond to check if it is a file and if a directory.

You can see how it works RewriteCond here: /a/102731/65558

2* Edition

My environment

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

Man .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]

Man /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>

That’s what I can do to help you, your problems with .img are vacant for me, have no way of knowing exactly what is happening.

  • Please avoid long discussions in the comments; your talk was moved to the chat

  • @Maniero why ?

  • Because here it is not a forum, the comments are not for discussions, for problem solving interactively. Whenever it gets long is because there is something wrong (question or answer are bad and is not helping) and the system signals.

  • @Maniero roger con, vlw

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'] ) : ""; ?>
  • I put your rewrite but when entering the site you lost css. what might have occurred? is php7.1, if it helps.

  • ah yes, leave the directories of your Assets out of the watering, eg: Rewritecond %{REQUEST_URI} ! /Assets/ Rewritecond %{REQUEST_URI} ! /arquivos/ Obs: I updated my comment

  • I put the css in the 2nd line and it worked. But the images are distributed in each type of course and it is almost impossible to relate everything in Rewritecond. There is no general command like, "Disregard

  • has yes, os: Rewritecond %{REQUEST_URI} ! -f Rewritecond %{REQUEST_URI} ! -d

  • I put your rewrite now and the site lost css. Is there something interfering? I should post the . htaccess full for you to analyze?

  • I revised the text of the question and includes the structure of the site to help with the analysis.

Show 1 more comment

0

I decided as follows:

No . 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 a . htaccess with Rewrite above

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

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

Works perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.