How do I set apache to ignore the directory in the url?

Asked

Viewed 350 times

0

I have a directory inside my root folder called "mydir" where there are several files, currently I access these files so:

http://mysite.com/mydir/myarchive.html

My question is I have how to configure . htaccess so that I can access (show to the user) these files so:

http://mysite.com/myarchive.html

But I don’t want to redirect all the files, only the ". html".

I’m trying on my . htaccess, but I get a 404:

RewriteRule "^/(.+)$" "/mydir/$1"

Where " /(.+)$" is the regular expression, which I read in the documentation of apache that is to say:

^ = anchor to start expression.

/ = project root.

(.) = means any character.

+ = repeat "." once or more.

/mydir/ = directory which I want to replace.

$1 = variable referring regular expression in parentheses.

$ = anchor to end the expression.

And yet, when I try to access http://mysite.com/myarchive.html, I get a 404 error from page not found. Note that I haven’t even taken only html, but I imagine it would be something like " /(..html)$" where you need to escape . with a backslash.

  • https://answall.com/questions/89342/rewrite-como-redirecionar-tudo-sem-index-php

  • @Valdeirpsr I’m not sure if that’s it, I’ll see.

  • @Valdeirpsr is not that, it’s different, the files are being created in the directory, I can’t keep changing the . htaccess for each file.

2 answers

0

I believe it’s something like that:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(.*)/ 
RewriteCond %{REQUEST_FILENAME} /.+\.html$
RewriteRule (.+) /mydir/$1 [QSA, L]

This condition takes any file .html at the root and rewrites the URL to /mydir/arquivo.html

  • But the question is exactly the opposite, I need to take all of "mydir" and be able to access this http://mysite.com/myarchive.html instead of http://mysite.com/mydir/myarchive.html, but it already helps, I will see here.

  • Internal Server Error! 500, is giving error in the syntax of this line here RewriteRule (.+) /mydir/$1 [QSA, L]

0

After hours of trying I got it. Just do so on your . htaccess, I will explain:

RewriteEngine On

RewriteCond %{REQUEST_URI} !/mydir/
RewriteRule "^(.+\.html)$" "/mydir/$1"

Rewriteengine On

Activates the rewrite mode.

RewriteCond %{REQUEST_URI} !/mydir/ 

To create a condition and deny "/mydir/" in the loop, if something like: /mydir/mydir/mydir/mydir/mydir/mydir/mydir/mydir/mydir/mydir/

RewriteRule "^(.+\.html)$" "/mydir/$1" 

And last but not least, the rewrite rule, where " (.+.html)$" is the regular expression that means it will take all the files. html, notice the need to escape the point, because the same is a regular expression character meaning "any character", the + is to repeat the point once or more.

Browser other questions tagged

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