3

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

enter image description here

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Sophie
  • 410
  • 3
  • 10
  • Hey rs someone ;( – Sophie Nov 17 '21 at 07:32
  • And can that logic get applied blindly to _all_ requests? Or are there exceptions? – arkascha Nov 17 '21 at 08:16
  • And a side note: creating 10k files inside a single folder is _not_ an option. Regardless of whether it is a good idea to create auch an amount of static files (it typically is _not_ ) you need to consider that there are limitations in the physical file systems. – arkascha Nov 17 '21 at 08:22
  • Hmm, so what do you suggest to me? Its 10k because I am doing that to separate... otherwise it would be 25k inside one folder.. =/ – Sophie Nov 17 '21 at 08:55

2 Answers2

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Hey Anubhava thank you soo much for your answer, your code works but now I am having trouble to open other pages, which are located in root directory, when I request it appear "[an error occurred while processing this directive]" – Sophie Nov 18 '21 at 19:51
  • Hey Anubhava, Sorry for the inconvenience, but could you tell me what I need to change to rewrite the access for another path instead of root? - I tried to change the line: RewriteCond %{HTTP_HOST} avantitecnologiati [NC] to RewriteCond %{HTTP_HOST} avantitecnologiati.com.br/anotherpath [NC] but got no lucky rs ;( – Sophie Jan 20 '22 at 05:13
  • Hey Anubhava, thank you soo much for your answer, but it didn't worked, this is the section I did: (line one) RewriteCond %{HTTP_HOST} =avantitecnologiati.com.br (line two) RewriteCond %{REQUEST_URI} ^/anotherpath (line three) RewriteCond %{DOCUMENT_ROOT}/anotherpath/estado/$1.php -f (line four) RewriteRule ^([\w-]+)/?$ anotherpath/estado/$1.php [L] ------- is that ok? – Sophie Jan 21 '22 at 02:12
  • 1
    Hey Anubhava, how are you? Long time after using this rules I figure out that when I have a page with the same name (example: blog.php) if I have any entry /blog/noexist/ the htaccess turn into a loop putting .php.php.php and doesnt redirect to 404 error do you know how can I prevent that? – Sophie Jan 11 '23 at 04:58
  • Here is two links, if you could check: Error: www.avantitecnologiati.com.br/blog/noexisturl/ Normal: avantitecnologiati.com.br/blog/ The file blog.php exists on root directory, but the file noexisturl.php doesnt exist inside any path called blog – Sophie Jan 11 '23 at 05:34
  • Is the last paragraph for add .php extension? Because the original htaccess force to remove the .php extension – Sophie Jan 11 '23 at 06:23
  • But I dont need to add .php extension I need to keep like before, removing .php extension – Sophie Jan 11 '23 at 06:32
  • I see but I really dont need to add .php extension at URL's I need to remove the .php at urls – Sophie Jan 11 '23 at 06:39
  • 1
    It does removal of `.php` but I think you are not getting it. Check https://stackoverflow.com/posts/70024819/revisions to understand the minor difference between old and new answer. – anubhava Jan 11 '23 at 06:41
  • 1
    Oh I see, I am so sorry to waste your time with my mistake, I had misunderstood. – Sophie Jan 11 '23 at 06:51
  • Your rule works perfectly, like always... Just one detail please, I had to add one rule to force trailing slash because google was considering duplicates pages... so , my question is: is it possible to do one redirect when requestion non trailing slash? example: www.avantitecnologiati.com.br/blog/noexisturl/ >> works perfectly but ## www.avantitecnologiati.com.br/blog/noexisturl >> makes two jumps (301) – Sophie Jan 11 '23 at 06:57
  • I think at this stage it is better to post a new problem for 2 redirect problem with few examples. – anubhava Jan 11 '23 at 09:40
  • 1
    Hey Anubhava, I am getting a 301 looping when trying to add the trailing slash lol.. damm that .htaccess is killing me ;( could you check my question? https://stackoverflow.com/questions/75093583/add-trailing-slash-at-dynamic-htaccess – Sophie Jan 12 '23 at 08:45
  • I am still waiting for you hehe, I created another question to show all the content of the .htaccess file :) – Sophie Jan 13 '23 at 09:23
0

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

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Hey, thanks for your answer but it didn't worked ;( I got a big loop and in the end an 302 – Sophie Nov 17 '21 at 09:04
  • Sorry, indeed, I added a condition to break the rewriting loop. – arkascha Nov 17 '21 at 09:05
  • Still not working, can it be for my other rule wich removes the php extension? – Sophie Nov 17 '21 at 09:11
  • #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] – Sophie Nov 17 '21 at 09:11
  • Sure, those two separate rules certainly are in conflict. This is why it always makes sense to provide _all_ information in the question itself. You will have to combine those aspects. – arkascha Nov 17 '21 at 09:27
  • sorry for that, I have updated the question with the real content in my .Htaccess – Sophie Nov 17 '21 at 09:37
  • Where is that ".htaccess" file located? Top level or inside that "folder"? – arkascha Nov 17 '21 at 10:00
  • Located at root path – Sophie Nov 17 '21 at 10:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239308/discussion-between-sophie-and-arkascha). – Sophie Nov 17 '21 at 10:05