Need help with Apache Rewrite issues - apache

My developer has provided me some Apache rewrite rules that are required for our application to work. When I added them to Apache my www.domain.com/blog and www.domain.com/phpmyadmin pages no longer worked. I tried to add the first RewriteCond rule for my blog and also the final phpmyadmin rule but neither one is working as expected. Essentially I want any requests to /blog or /phpmyadmin to NOT rewrite and go to my document root directory and run those applications outside of rewrites. Can you help me figure out a solution? Thanks
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^/(.*_css.*\.css.*) /$1 [QSA,L]
RewriteRule ^/(.*_js.*\.js.*) /$1 [QSA,L]
RewriteRule ^/(.*_swf.*\.swf.*) /$1 [QSA,L]
RewriteRule ^/(.*_img.*\.[jpg|JPG|jpeg|JPEG|gif|GIF|bmp|BMP|png|PNG].*) /$1 [QSA,L]
RewriteRule ^/(.*)$ /index.php?url=$1 [QSA,L]
RewriteRule ^/phpmyadmin(.*)$ /phpmyadmin$1 [QSA,L]
</VirtualHost>
They are located at www.domain.com/blog and www.domain.com/phpmyadmin.
Apache 2.2.13
Thanks!

If you want to avoid rewriting URLs that begin with /blog/ or /phpmyadmin/ then you may be able to get away with the first rule being:
RewriteRule ^/(?:blog|phpmyadmin)/ - [L]
(replacing your RewriteCond and then removing the old phpmyadmin rule).
A quick explanation: this matches any URLs that begin with /blog/ or /phpmyadmin/ and doesn't rewrite them (- for the replacement), and then stops any further rewriting ([L]).
Previous answer:
Your rewrite rules are conflicting, for a start. Also, at the moment, the RewriteCond only applies to the first rule. Also, the final rule is ignored because of the rule before it matching everything. You may want something like this:
RewriteRule ^/blog/(.*)$ /index.php?url=$1 [QSA,L]
as I assume you're just trying to rewrite all URLs from /blog/foo/bar to /index.php?url=foo%2fbar? If not, please explain what you're trying to accomplish and I'll edit my answer.

Err... not sure what you are trying to do. Are you trying to "Exclude" static files as images/stylesheets/flash movies/javascript files and the link to PHPMyAdmin? Maybe something like this could do the job for you 'quick 'n dirrrrty'
# Enable the RewriteEngine
RewriteEngine On
RewriteBase /
# Check if the file, directory or symlink does not already exists.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-F
RewriteCond %{REQUEST_FILENAME} !favicon\.ico
RewriteCond %{REQUEST_FILENAME} !robots\.txt
# Rewrite all other requests to the index.php
RewriteRule ^(.+)$ /index.php?url=$1 [QSA,L]

Related

Apache RewriteRule not working – page not found

I have no idea why it doesn't work, /spelling/30000 gives the Not Found page. Please help.
RewriteEngine On
RewriteBase /
RewriteRule ^/([0-9]+)$ /?mod=spelling&word=$1 [PT]
RewriteRule ^$ /?mod=spelling [PT]
With your shown samples please try following .htaccess rules file. Considering that index.php file is the one which is taking traffic in backend for internal rewrite rules. Please make sure to keep your .htaccess file along side with index.php file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteRule ^/?$ index.php?mod=spelling [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?mod=$1&word=$2 [QSA,L]

apache mod_rewrite - rewrite rule

i have a problem with an mod rewrite rule in the WOULD NOT WORK area :)
I have an Project site witch is just an ajax return site.
So i like to open my site on sitename.com/my-test/project/test.html and would have a rewrite to sitename.com/my-test/project.html?index=test.
I know after my rewrite the rewrite rules of the cms must going his work. Im not sure maybe this is the problem and they cross or crash.
If i browse the URLs via browser all will work just fine, only the rewrite rule would not work and i become total crazy and cant find the problem.
So i hope someone can help me.
REWRITE CONFIG in .htaccess
RewriteEngine On
RewriteBase /
#### WOULD NOT WORK ######
# REWRITE AJAX PROJEKT
RewriteCond %{REQUEST_URI}% ^my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
RewriteRule ^my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
### WORK CORRECTLY ####
# REWRITE RULE FOR SEO FRIENDLY IMAGE MANAGER URLS
RewriteRule ^files[0-9]*/imagetypes/([^/]*)/([^/]*) index.php?rex_img_type=$1&rex_img_file=$2
# DON'T REWRITE IF REAL FILE, FOLDER OR SYMLINK EXISTS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# EXCLUDE SPECIFIC FOLDERS FROM REWRITE ENGINE
RewriteCond %{REQUEST_URI} !/files[0-9]*/
RewriteCond %{REQUEST_URI} !/assets/.*
RewriteCond %{REQUEST_URI} !/media/.*
RewriteCond %{REQUEST_URI} !/redaxo/.*
# REWRITE ALL OTHER REQUESTS TO INDEX.PHP
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
and i also have tested
RewriteCond %{REQUEST_URI}% ^/my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
RewriteRule ^/my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
#OR JUST
RewriteRule ^/my-test/projects/[a-zA-Z0-9]+\.html$ my-test/projects.html?index=$1
#OR
RewriteRule ^/my-test/projects/(.*)$ my-test/projects.html?index=$1
#AND CORRECT
RewriteCond %{REQUEST_URI} ^/my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
RewriteRule ^/my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
#AND ---
RewriteRule ^my-test/projects/([a-zA-Z0-9]+)\.html my-test/projects.html?index=$1 [L]
I dont find the answer, so i realy hope someone can help me
Thank you all
ng
moxx
RewriteCond %{REQUEST_URI}% ^my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
RewriteRule ^my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
You have an extra % at the end of your condition: %{REQUEST_URI}%. That means it must be the request URI followed by a %, and you text on the right ends with html, which means this condition will always fail.
Additionally, the %{REQUEST_URI} variable starts with a /. Note that the leading slash is gone when used to match the pattern in the rewrite rule.
You actually don't need the condition at all.
RewriteRule ^my-test/projects/([a-zA-Z0-9]+)\.html my-test/projects.html?index=$1 [L]

Mod Rewrite not working correctly

I have written a simple mod rewrite script to turn website.com/index.php?var1=1&var2=2&var3=3 into website.com/index/1/2/3. For some reason it's not working, any ideas why?
#RewriteRule ^$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1\.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ comments.php?var1=$1&var2=$2&var3=$3
The first rule rewrites index/1/2/3 to index/1/2/3.php.
then the second rule rewrites this to index.php/index/1/2/3.php
and finally the last rule doesn't match.
If you want to rewrite website.com/index.php?var1=1&var2=2&var3=3 into website.com/index/1/2/3, you must capture the query string arguments with RewriteCond and insert them into a RewriteRule
RewriteEngine on
RewriteCond %{QUERY_STRING} var1=(.*?)&var2=(.*?)&var3=(.*?)
RewriteRule ^index.php$ /index/%1/%2/%3 [L]
If you want to redirect instead of internally rewrite, i.e. show the new URL in the browser, use [R,L] as RewriteRule flags
RewriteRule ^index.php$ /index/%1/%2/%3 [R,L]
It looks like the rewrite on line 6 will catch that and send it to index.php. It's hard to tell not knowing what things are files and directories, but I would guess that it isn't. If you have access to the httpd.conf you can turn up the rewrite logging, set
RewriteLogLevel 8
And it will log far more information than you need to debug this. On newer versions of apache the setting is set with LogLevel e.g..
LogLevel alert rewrite:trace8
which may be allowed in vhosts or htaccess so look it up in your version's docs.
Don't leave this on as it will seriously impact performance.

.htaccess rewrite to default language folder?

I have my site broken down into several folders by language:
/
/en/
index.php
about-us.php
faq.php
...
/fr/
index.php
about-us.php
faq.php
...
...
etc.
I'd like to have a rewrite rule that automatically rewrites to the en folder if somebody tried to enter mydomain.com/about-us.php.
FYI, I also already have a rewrite rule in place that removes the extension, so really I want to make sure that mydomain.com/about-us rewrites to mydomain.com/en/about-us. Here's my existing rewrite rule that does this:
# allows for extension-agnostic urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
That make sense? Anyone help me out here?
EDIT:
#Gumbo -
Here's what my .htaccess file looks like (this is all that's in it):
RewriteEngine On
# defaults to the english site
RewriteRule !^[a-z]{2}/ /en%{REQUEST_URI} [L,R=301]
# allows for extension-agnostic urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
This file is in the root of my website (not in a language folder). I've reloaded my webserver, just to be on the safe side, and I still get this error message when I try to go to mydomain.com/about-us:
Not Found
The requested URL /about-us was not found on this server.
... where mydomain.com/en/about-us works just fine.
Any other thoughts?
Put this rule before your rule:
RewriteRule !^(fr|en)/ /en%{REQUEST_URI} [L,R=301]
Or more general:
RewriteRule !^[a-z]{2}/ /en%{REQUEST_URI} [L,R=301]

Multiple RewriteConds and RewriteRule Stacked Together

I have this apache rewrite rule:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} mycompany.com
RewriteRule ^$ http://mycompany.com/login [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
The only thing I can make sense of is if it's mycompany.com, then the script will redirect to http://mycompany.com/login. If not, then ...
I can't figure out already.
Any idea what does the above script say?
Something quite interesting, not easy to understand.
A google search on the comment texts inside the code gave interesting results: http://www.google.com/search?q=%22%23+we+check+if+the+.html+version+is+here+%28caching%29%22
Edit: if we look at the last lines and knowing that Symfony uses caching (it creates local files with .html extension in the same directories as the URL shows 'em) I can try to explain the lines here
If the requested url is something like http://yoursite.com/blabla/ we try to open an index.html file in that directory. If the file is not there, another cycle of rewriting will happen and the last Cond will be hit (where the file does not exist)
RewriteRule ^$ index.html [QSA]
If something more is in the url, like http://yoursite.com/blabla/blblbl, try to find a file blblbl.html
RewriteRule ^([^.]+)$ $1.html [QSA]
This is the collector of all urls that did not match any of the previous rules or the cached file did not exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]