How to hide directory paths in htaccess url?

Asked

Viewed 7,154 times

2

How to make all site urls shortened by hiding all directory paths?

For example:

www.meusite.com/models/scripts/arquivo.php
www.meusite.com/view/templates/arquivo.php
www.meusite.com/view/templates/especifica/arquivo.php

And I want you to stay forever:

www.meusite.com/arquivo.php

Is that possible? How to do it? (already in advance I read all, or almost all the main questions about it here on the site and did not find an objective answer).

Update: What I’m looking for is a rule with regular expression that always does this, in all urls, in the answer line and comments of the @Miguel Neto response.

  • 1

    In this case I believe, that you would have to put your files at the root of your project.

  • @Marconi I think I expressed myself badly. In case I have a url https://www.meusite.com.br/pasta1/pasta2/pasta3/arquivo.php for example, and I want it to appear on the navigation bar only https://www.meusite.com.br/arquivo.php. This is not possible with htaccess?

  • 1

    In the case of the answer below, you can use a regex that reaches the expected result.

  • 1

    Unless you specify the path to each file on .htaccess, or create file name patterns such as lib_arquivo.php that will redirect to lib/arquivo.php cannot be done only with regex. Unfortunately, this is not possible due to lack of information. There is no way to recover the folder of a particular file from a URL without having it mapped. Another alternative is to use a language and implement Friendly URL.

  • Because it is @marcusagm actually I was making a mess to structure the folders and files of my project... so the solution was to change the structure of the folders and put these files in the same root, and then I no longer need to hide the paths... I think this comment of yours is actually an answer, because it solves my question ("Is it possible?"). Cheer up put there as an answer so I can close the topic. Thanks.

  • 1

    As it is imagined, because the path that was trying to do without specifying by htaccess file by file, would result in the reverse path, the URL presenting the folder but the file was at the root rs

Show 1 more comment

3 answers

2

You can use . htaccess to rewrite the URL

Current URL

www.meusite.com/arquivos/arquivo.php

.htaccess

RewriteEngine on
RewriteRule ^arquivo\.php$ arquivos/arquivo.php

URL

www.meusite.com/arquivo.php

You should put . htaccess in the directory of the file in question

  • So that’s right, but there’s no way to make a rule just for all urls?

  • 1

    Yes, there you have to take as a basis the root of the documents (domain). Doing with regex, the logic would be: URL(www.meusite.com/files/.php file), it will get the come before the ". php" and what comes after the last "/" bar and concatenate with what comes before the first "/" bar, getting (www.meusite.com/.php file). You can do it with regex, I just can’t tell you how exactly, I think it already helps.

  • Help yes, of course, thanks!

2


Unless you specify the path to each file on .htaccess, or create file name patterns such as lib_arquivo.php that will redirect to lib/arquivo.php there is no way to do only with regex.

Unfortunately, this is not possible due to lack of information. There is no way to recover the folder of a particular file from a URL without having it mapped.

Another alternative is to use a language and implement Friendly URL.

1

I don’t know if this is your case, but there is also a good way to hide the list of files from your server doing so:

Options -Indexes
  • 1

    Thanks Wallace, but this problem already solved (and with this same solution), as I said in this other question. What I’m looking for here is what @Miguel said in the comment of the other reply...

  • 1

    So the answer accepted was Miguel’s? Because I see another answer accepted.

Browser other questions tagged

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