Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to do a mod_rewrite redirection to relative URL
    primarykey
    data
    text
    <p>I am trying to achieve a basic URL redirection for pretty-URLs, and due to images, CSS etc. also residing in the same path I need to make sure that if the URL is accessed without a trailing slash, it is added automatically.</p> <p>This works fine if I put the absolute URL like this:</p> <pre><code>RewriteRule ^myParentDir/([A-Z0-9_-]+)$ http://www.mydomain.com/myParentDir/$1/ [R,nc,L] </code></pre> <p>But if I change this to a relative URL, so that I don't have to change it each time I move things in folders, this simply doesn't work.</p> <p>These are what I tried and all do not work, or redirect me to the actual internal directory path of the server like /public_html/... :</p> <pre><code>RewriteRule ^myParentDir/([A-Z0-9_-]+)$ ./myParentDir/$1/ [R,nc,L] RewriteRule ^myParentDir/([A-Z0-9_-]+)$ myParentDir/$1/ [R,nc,L] </code></pre> <p>What is the right way to do a URL redirection so that if the user enters something like:</p> <pre><code>http://www.mydomain.com/somedir/myVirtualParentDir/myVirtualSubdir </code></pre> <p>he gets redirected to (via HTTP 301 or 302):</p> <pre><code>http://www.mydomain.com/somedir/myVirtualParentDir/myVirtualSubdir/ </code></pre> <p>Thanks.</p> <p><strong>EDIT</strong>: Adding some more details because it does not seem to be clear. Lets say I am implementing a gallery, and I want to have pretty URLs using mod_rewrite. So, I would like to have URLs as follows:</p> <pre><code>http://www.mydomain.com/somedir/galleries/cats </code></pre> <p>which shows thumbnails of cats, while:</p> <pre><code>http://www.mydomain.com/somedir/galleries/cats/persian </code></pre> <p>which shows one image from the thumbnails of all cats, named persian. So in actual fact the physical directory structure and rewriting would be as follows:</p> <pre><code>http://www.domain.com/somedir/gallery.php?category=cats&amp;image=persian </code></pre> <p>So what I want to do is put a .htaccess file in /somedir which catches all requests made to /galleries and depending on the virtual subdirectories following it, use them as placeholders in the rewriting, with 2 rewrite rules:</p> <pre><code>RewriteRule ^galleries/(A-Z0-9_-]+)/$ ./gallery.php?category=$1 [nc] RewriteRule ^galleries/(A-Z0-9_-]+)/+([A-Z0-9_-]+)$ ./gallery.php?category=$1&amp;image=$2 [nc] </code></pre> <p>Now the problem is that the gallery script in fact needs some CSS, Javascript and Images, located at <a href="http://www.domain.com/somedir/css">http://www.domain.com/somedir/css</a>, <a href="http://www.domain.com/somedir/js">http://www.domain.com/somedir/js</a>, and <a href="http://www.domain.com/somedir/images">http://www.domain.com/somedir/images</a> respectively. </p> <p>I don't want to hardcode any absolute URLs, so the CSS, JS and Images will be referred to using relative URLs, (./css, ./js, ./images etc.). So I can do rewriting URLs as follows:</p> <pre><code>RewriteRule ^galleries/[A-Z0-9_-]+/css/(.*)$ ./css/$1 [nc] </code></pre> <p>The problem is that since <a href="http://www.domain.com/somedir/galleries/cats">http://www.domain.com/somedir/galleries/cats</a> is a virtual directory, the above only works if the user types:</p> <pre><code>http://www.domain.com/somedir/gallaries/cats/ </code></pre> <p>If the user omits the trailing slash mod_dir will not add it because in actual fact this directory does not actually exist.</p> <p>If I put a redirect rewrite with the absolute URL it works:</p> <pre><code>RewriteRule ^galleries/([A-Z0-9_-]+)$ http://www.mydomain.com/subdir/galleries/$1/ [R,nc,L] </code></pre> <p>But I don't want to have the URL prefix hardcoded because I want to be able to put this on whatever domain I want in whatever subdir I want, so I tried this:</p> <pre><code>RewriteRule ^galleries/([A-Z0-9_-]+)$ galleries/$1/ [R,nc,L] </code></pre> <p>But instead it redirects to:</p> <pre><code>http://www.mydomain.com/home/myaccount/public_html/subdir/galleries/theRest </code></pre> <p>which obviously is not what I want.</p> <p><strong>EDIT:</strong> Further clarifications</p> <p>The solution I am looking for is to avoid hardcoding the domain name or folder paths in .htaccess. I am looking for a solution where if I package the .htaccess with the rest of the scripts and resources, wherever the user unzips it on his web server it works out of the box. All works like that apart from this trailing slash issue.</p> <p>So any solution which involves hardcoding the parent directory or the webserver's path in .htaccess in any way is not what I am looking for. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload