Home > OS >  Dynamic .htaccess rule - redirect root
Dynamic .htaccess rule - redirect root

Time:11-19

I need some help to transform my static .htaccess rule to a dynamic rule. This is the definition of the rule:

#Redirect the request of file inside folder 'agencias/pagina_agencia/' to the base root URL
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.avantitecnologiati.com.br
RewriteRule ^teste2 agencias/pagina_agencia/teste2.php [L,QSA]

Everytime when someone request the page: [www.avantitecnologiati.com.br/teste2] automatically the htaccess rule reads the file called "teste2.php" which are located at this path:[www.avantitecnologiati.com.br/agencias/pagina_agencia/teste2.php] everything working perfect right?

Nop, I got two problems ;(

  1. I need to create a new line everytime I add a new file inside the folder called "agencias/pagina_agencia/", something like:
RewriteRule ^teste3 agencias/pagina_agencia/teste3.php [L,QSA]

And that's not functional, because I am going to create 10k files inside the folder "directory"...

  1. If the user directly requests [www.avantitecnologiati.com.br/agencias/pagina_agencia/teste2], it needs to redirect to the page [www.avantitecnologiati.com.br/teste2] and wouldn't display the real location...

Thanks for your time to read my question ;)

FULL HTACCESS CODE

RewriteEngine On
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ avantitecnologiati.com.br$1 [R=301,L]

RewriteBase /
RewriteCond %{HTTP_HOST} ^www.avantitecnologiati.com.br
RewriteRule ^teste2 agencias/pagina_agencia/teste2.php [L,QSA]
RewriteRule ^teste3 agencias/pagina_agencia/teste3.php [L,QSA]

#Hide and Redirect Extension
RewriteEngine on 
RewriteCond %{THE_REQUEST} ^[A-Z] \s. \.php\sHTTP/. 
RewriteRule ^(. )\.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/shtml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml xml
AddOutputFilterByType DEFLATE application/rss xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

#Custom Error Page
ErrorDocument 404 http://www.avantitecnologiati.com.br/

I am getting a 302 error when try to use the arkascha rule's, when I request the url: enter image description here

CodePudding user response:

This probably is what you are looking for:

RewriteEngine on
RewriteRule ^/?directory/([^/] )\.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/directory/
RewriteRule ^ /directory%{REQUEST_URI}.php [L]

It is a good idea to start out with a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues ...

CodePudding user response:

Have it like this (see inline comments) in your site root .htaccess:

RewriteEngine On

# external redirect rule to remove /agencias/pagina_agencia/ from URLs
RewriteCond %{THE_REQUEST} \s/ agencias/pagina_agencia/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]

#Hide and Redirect Extension
RewriteCond %{THE_REQUEST} ^[A-Z] \s. \.php\sHTTP
RewriteRule ^(. )\.php$ /$1 [R=301,L]

# internal rewrite from root to /agencias/pagina_agencia/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteRule ^([\w-] )/?$ agencias/pagina_agencia/$1.php [L]

# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(. ?)/?$ $1.php [L]

Note about relative URLs for including resources in your HTML:

You may hit the most common problem people face when switching to pretty URL schemes i.e. resource not found when using relative paths. Simple solution is to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.

Otherwise You can add this just below <head> tag of your page's HTML: <base href="/" /> so that every relative URL is resolved from that base URL and not from the current page's URL.

  • Related