Forum Moderators: phranque
I'm a newbie to mod rewrite and I'm trying to figure some stuff out. I know my code is probably really really bad, but here it is:
Options +FollowSymlinks
RewriteEngine on
# If requested resource exists as a file or directory, then do not rewrite
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
# Rewrite the sitemanager directory to use the modules directory
RewriteRule ^sitemanager/([^/]+)(.*)$ modules/$1/admin/$2 [L]
# If requested resource exists as a file or directory, then do not rewrite
RewriteCond /new/%{REQUEST_FILENAME} -f [OR]
RewriteCond /new/%{REQUEST_FILENAME} -d
RewriteRule . - [L]
# Rewrite the root to redirect to the webroot (there's 2 for the slashes)
RewriteRule ^(.*)/$ /new/webroot/$1/ [QSA,L]
RewriteRule ^(.*)$ /new/webroot/$1/ [QSA,L]
First /new/ is the current rootpath, that will change to just / after I'm done the project. Basically what I want to do is rewrite all public requests to go to /new/webroot/ rather than having 15 folders right on my root path. I have a modules folder on my root that I want to use, and inside I have various folders that I want to use through the project. This will enable me to move "modules" that I have in and out of the project.
In my /new/webroot/ folder I have this:
Options +FollowSymlinks
RewriteEngine on
# If requested resource exists as a file or directory, then do not rewrite
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
# If requested resource exists as a file or directory, then do not rewrite
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/templates/(.*)$ /new/modules/$1/templates/$2 [L]
RewriteRule ^(.*)/js/(.*)$ /new/modules/$1/css/$2 [L]
RewriteRule ^(.*)/css/(.*)$ /new/modules/$1/css/$2 [L]
RewriteRule ^(.*)/images/(.*)$ /new/modules/$1/images/$2 [L]
RewriteRule ^(.*)/(.*)/$ /new/modules/$1/public/$2 [QSA,L]
RewriteRule ^(.*)/(.*)$ /new/modules/$1/public/$2 [QSA,L]
I hope my code doesn't make you vomit, but please be kind as I'm a total newbie to this.
The problem is, the images won't work, the css won't work, etc. It seems to be rewriting correctly for the most part, but sometimes if I go /new/sitemanager/whatever/whatever.html, I'll get a 500 error. Also, instead of getting 404's on pages that don't exist, I get 500 errors.
Can anyone give me advice on how to properly write this? And what suggestions you give for me to make my own module based system?
I would appreciate it, thank you.
If you have added "virtual directories" in your new URLs to carry data for use in your script query string parameters, then the browser will resolve any page-relative object links on your pages according to the directory-level that is showing in its address bar.
Therefore, you may need to change the CSS, external JavaScript, and image links to reflect their location with respect to the URL in the client's address bar, or you will need to change those links to server-relative or canonical links instead of page-relative links -- i.e. use <img src="/images/logo.gif"> or <img src="http://example.com/images/logo.gif">, instead of <img src="images/logo.gif"> or <img src="../images/logo.gif">
Jim