Jump to content

mod_rewrite to different directory


Go to solution Solved by fastsol,

Recommended Posts

I am trying to handle 404 errors to mainly pdf files that used to reside in a different directory but have now moved permanantly to a new directory.  I have tired many variations for the rewrite and none have worked or even given me errors to make me think I am kind on the right track.

 

This is the line I have tried using

RewriteRule ^/files/starter-manuals(.*) /admin_files/PDF$1 [R=301,L]

This is an example of a url that should be redirected http://remotelystartedmn.com/files/starter-manuals/RS625.pdf

Here is a copy of the whole htaccess just in case something else if mking it not work.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.remotelystartedmn.com$ [NC]
RewriteRule ^(.*)$ http://remotelystartedmn.com/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^product_id=(.*)$
RewriteRule . %{REQUEST_URI}?product-id=%1 [R=301,L]

RewriteBase /
RewriteCond %{HTTP:Cookie} devicePixelRatio [NC]
RewriteRule \.(?:jpe?g|gif|png|bmp)$ /retinaimages.php [NC,L]

RewriteRule ^/files/starter-manuals/(.*) /admin_files/PDF/$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-s 
RewriteRule ^(.*)\.[\d]+\.(css|js)$ $1.$2 [L] 

ErrorDocument 404 /404.php

# ----------------------------------------------------------------------
# Expires headers (for better cache control)
# ----------------------------------------------------------------------
 
#
# These are pretty far-future expires headers
# They assume you control versioning with cachebusting query params like:
#   <script src="application.js?20100608">
# Additionally, consider that outdated proxies may miscache
#
#   www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/
 
#
# If you don`t use filenames to version, lower the css and js to something like "access plus 1 week"
#
 
<IfModule mod_expires.c>
  ExpiresActive on
 
# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault                          "access plus 1 month"
 
# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest       "access plus 0 seconds"
 
 
 
# Your document html
  ExpiresByType text/html                 "access plus 0 seconds"
   
# Data
  ExpiresByType text/xml                  "access plus 0 seconds"
  ExpiresByType application/xml           "access plus 0 seconds"
  ExpiresByType application/json          "access plus 0 seconds"
 
# RSS feed
  ExpiresByType application/rss+xml       "access plus 1 hour"
 
# Favicon (cannot be renamed)
  ExpiresByType image/x-icon              "access plus 1 week"
 
# Media: images, video, audio
  ExpiresByType image/gif                 "access plus 1 month"
  ExpiresByType image/png                 "access plus 1 month"
  ExpiresByType image/jpg                 "access plus 1 month"
  ExpiresByType image/jpeg                "access plus 1 month"
  ExpiresByType video/ogg                 "access plus 1 month"
  ExpiresByType audio/ogg                 "access plus 1 month"
  ExpiresByType video/mp4                 "access plus 1 month"
  ExpiresByType video/webm                "access plus 1 month"
   
# HTC files  (css3pie)
  ExpiresByType text/x-component          "access plus 1 month"
   
# Webfonts
  ExpiresByType font/truetype             "access plus 1 month"
  ExpiresByType font/opentype             "access plus 1 month"
  ExpiresByType application/x-font-woff   "access plus 1 month"
  ExpiresByType image/svg+xml             "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
     
# CSS and JavaScript
  ExpiresByType text/css                  "access plus 1 month"
  ExpiresByType application/javascript    "access plus 1 month"
  ExpiresByType text/javascript           "access plus 1 month"
   
  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>
   
</IfModule>

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript

I have also tried putting the line in question above the RewriteBase, putting an absolute url for both directories and removed/moved slashes before/after just about everything but nothing has worked.

 

Any insight is appreciated!

Link to comment
https://forums.phpfreaks.com/topic/289539-mod_rewrite-to-different-directory/
Share on other sites

Your first Rule suggests that the URL you're matching against does not start with a slash. (They may or may not depending on your setup.) So try removing that from the new one.

 

Easier would be to use the Redirect directive instead.

Redirect permanent /files/starter-manuals /admin_files/PDF

I tried you other method and still does not redirect.  No matter what I do all I get is the 403 Forbidden page.  I have tried so many variations with and without slashes I can't even remember all of them at this point.  Here are a few that I can remember now.

RewriteRule ^/files/starter-manuals/(.*)$ /admin_files/PDF/$1 [R=301,L]
RewriteRule ^/files/starter-manuals/(.*)$ admin_files/PDF/$1 [R=301,L]
RewriteRule ^files/starter-manuals/(.*)$ admin_files/PDF/$1 [R=301,L]
RewriteRule ^files/starter-manuals/(.*)$ /admin_files/PDF$1 [R=301,L]
RewriteRule ^/files/starter-manuals/(.*)$ admin_files/PDF$1 [R=301,L]
RewriteRule ^files/starter-manuals/(.*)$ admin_files/PDF$1 [R=301,L]

I added the $ in the string above but that hasn't made a difference either.  I don't really know a lot about this type of stuff but am learning a little bit of how it works.

I don't remember Apache's request lifecycle very well but it may be trying to authenticate before it does the redirect. What are you using to set permissions on that directory? Can you lift them temporarily while you check if the redirection works?

I don't have any permissions set on the directory.  It's fully accessable from the url.

Old url - http://remotelystartedmn.com/files/starter-manuals/RS625.pdf that gives the 403 error

New url - http://remotelystartedmn.com/admin_files/PDF/RS625.pdf

  • Solution

Ok so I figured out what was stopping it from redirecting.  I have a htaccess in the "files" folder.  The htaccess only has this line in it.

DENY FROM ALL

Technically I could jut remove the htaccess file and all would work since I am not doing anything with the "files" folder at this time.  But if I do decide to use the folder in the future I would want the htaccess there.  So is there a way to make these things work together and get the redirect to work like I want?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.