开发者

PHP: mod_rewrite if query contains value

开发者 https://www.devze.com 2023-03-06 16:31 出处:网络
Rece开发者_如何学Cntly I solved my issue with mod_rewrite to redirect a domain to a subfolder. Now that I\'ve fixed my original issue, I\'ve hit a another wall.

Rece开发者_如何学Cntly I solved my issue with mod_rewrite to redirect a domain to a subfolder. Now that I've fixed my original issue, I've hit a another wall.

Here is an example structure:

/
/index.php
/content/
/content/styles.css
/domain/
/domain/index.php

For simplicity, /domain/ is the current and top-most folder. In /domain/index.php I am trying to access /content/styles.css. How can I accomplish this? Assume there is no web link to the previous directories. Also ../ does not work as ../ returns the same directory as ./.

I thought of a way, but my .htaccess skills aren't very strong and I don't want to spend hours or days piecing together an answer. Let's say I have:

<link rel="stylesheet" type="text/css" href="content/styles.css" />

If I am right, href performs a request for the file. How can I use .htaccess to capture the request and point it to the correct folder? Like if the query string looks like ^/content/(.*)$, and rewrite it back one directory to access ../content instead.

Hopefully this made some type of sense.


You could use an Alias:

<VirtualHost *:80>
    DocumentRoot "/domain"
    Alias /content "/content"
</VirtualHost>


You could create a dummy /domain/content directory with another .htaccess file which has something like

RewriteRule ^([^/]+)$ /content/$1 [NC,L]


if /content/ is NOT inside /domain/ and /domain/ is your DOCUMENT_ROOT on your webserver, then you can't access the /content/ folder at all using a <link> tag & mod_rewrite.

You could access it via PHP and include it's contents, but that's a different story.


I considered that;

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

exist in your .htaccess file. Why aren't you trying like that?

$dir = dirname(__FILE__);
$link = $_SERVER['HTTP_HOST'];
// $link = preg_replace('/^www\./i', '', $link);

for hrefs;

href="<?php echo $link . '/content/content.css'; ?>"

for imports;

include($dir . '/example.php');
0

精彩评论

暂无评论...
验证码 换一张
取 消