skip to Main Content

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.+.phpsHTTP/.+
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: http://www.avantitecnologiati.com.br/ it doesn’t find the file ;(

enter image description here

2

Answers


  1. 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 …

    Login or Signup to reply.
  2. Have it like this (see inline comments) in your site root .htaccess:

    Options -MultiViews
    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]
    
    # Remove .php extension externally
    # To externally redirect /dir/file.php to /dir/file
    RewriteCond %{THE_REQUEST} s/+(.+?).php[s?] [NC]
    RewriteRule ^ /%1 [R=301,NE,L]
    
    # internal rewrite from root to /agencias/pagina_agencia/
    RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
    RewriteCond %{DOCUMENT_ROOT}/agencias/pagina_agencia/$1.php -f
    RewriteRule ^([w-]+)/?$ agencias/pagina_agencia/$1.php [L]
    
    # handle .php extension internally
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search