3

How to get file directory trough .htaccess by using RewriteRule ^(.*)$ ?id=$1 [L,QSA]?

If .htaccess is located in http://localhost/some/dir/.htaccess and I'm opening http://localhost/some/dir/here/I/use/RewriteRule/, how I detect value /some/dir/ without using RewriteBase and without manual adding %{DOCUMENT_ROOT}/some/dir/, like value localhost I get trough %{HTTP_HOST}?

Binyamin
  • 7,493
  • 10
  • 60
  • 82

2 Answers2

0

If you do not use RewriteBase you need to tell mod-rewrite the real Directory Root /var/ww/mysite/some/dir in the rewrite rule. RewriteBase would take the location url and map it to the directory.

So you'll maybe end up with

 RewriteRule /var/ww/mysite/some/dir/(.*)$ ?id=$1 [L,QSA]

And trying to map some internal variables it may be

 RewriteRule %{DOCUMENT_ROOT}/some/dir/(.*)$ ?id=$1 [L,QSA]

But I'm unsure, I rarely use mod_rewrite in .htaccess -- I prefer Directory tags, and the file path management can be different in .htaccess (auto removal and adding of directory prefixes). If you do not find a solution try to ask Servfault, plenty of admins other there.

regilero
  • 29,806
  • 6
  • 60
  • 99
  • This is a hackish solution though. https://stackoverflow.com/questions/984554/how-to-reference-the-current-directory-from-htaccess-using-mod-rewrite#comment80066401_984641 Apache should give a better way to get the htaccess parent folder uri. – Pacerier Oct 04 '17 at 02:49
0

Actualy Apache still does not have pathinfo($,PATHINFO_DIRNAME), function like has PHP.

So on now there are solution on using %{REQUEST_URI}, like this example:

RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L]

may reset with:

RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,L]
Binyamin
  • 7,493
  • 10
  • 60
  • 82