bad path address with rewrite-rule - mod-rewrite

i'm already asked similar question month ago and dt recieve solution for my situation.
But now i have one important notice for this problem.
i'm running site on local web server. Path of site is http://ikot.
And when i enter adress like "ikot/news" - it's ok
but when i enter ikot/media, it transforms to "http://ikot/media/?level[0]=media&depth=1"
my rewrite rules in htaccess:
RewriteRule ^([0-9a-zA-Z_-]+)[/]?$ /index.php?level[0]=$1&depth=1 [L,QSA]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)[/]?$ /index.php?level[0]=$1&level[1]=$2&depth=2 [L,QSA]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)[/]?$ /index.php?level[0]=$1&level[1]=$2&level[2]=$3&depth=3 [L,QSA]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)[/]?$ /index.php?level[0]=$1&level[1]=$2&level[2]=$3&level[3]=$4&depth=4 [L,QSA]
the reason of this transformation (ikot/media to ikot/media/[garbage] path) is that i have "media" folder at my site's root folder. And maybe rewrite rule somehow considers, that i want to enter this folder or something like that (but ikot/media leads to media page of site, not to contents of media folder).
Help me pls with this situation.
Thanks

Please try switching off DirectorySlash. It looks that mod_dir changes yours url and rewrite doesn't work in way you want (first mod rewrite changes it to index.php?... and next mod dir redirects to /media/?... - or something like that).
Adding "DirectorySlash Off" to .htaccess works for me.
If it helps, please read carefully "Security Warning" on that page: http://httpd.apache.org/docs/2.0/mod/mod_dir.html

Related

Rewriterule won't work due to folder name conflict

I need to redirect mywebsite.com/admin to mywebsite.com/login.php, but the url in browser should stay mywebsite.com/admin. I use this rewrite rule:
RewriteCond %{REQUEST_URI} /admin$ [NC]
RewriteRule admin login.php [NC,L]
But it won't work because I have "admin" folder in my website root folder. It keeps entering the folder and listing files. When I delete "admin" folder, it works like a charm.
Seems that this message board is only for simple, dumb questions... Every time I've asked something complicated, no one answered. I'm wondering if there's even one expert participating in these discussions...

.htaccess image to web page redirect of sorts

I think this can be done, though not sure, so I came here to find out from you experts. I researched extensively, but always get the opposite of what I want to do. I was wondering if someone types in the image file URL (on server side) into an address bar if that could be directed to its corresponding web page.
For example:
One types in yourdomain.com/wp-content/uploads/image-name.jpg and it's directed to yourdomain.com/print/image-name (w/o .jpg)
So instead of one just seeing the image in a window, they will see the image, it's information and how to purchase it on my brand web site.
If that is not possible or the file name doesn't match, can directly typed server side image file URLs redirect to my website galleries or home page? And where in .htaccess would this code be located? Above begin wordpress or not?
If above Begin WP, will that interfere with this code that already exists? If so how would I exactly write it?
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sitename.com/$1 [R,L]
Thanks in advance!
EDIT: To clarify, my site being down has nothing to do with the help I received here. I upgraded my web host account and there was a glitch when they migrated it.
Insert this rule just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /wp-content/uploads/(.+?)\.jpg [NC]
RewriteRule ^ /print/%1? [L,R=302,NE]

Two parametrs in mod_rewrite rule

I have problem with mod_rewrite rule.
I'm trying to get adress:
www.site.com/en/page.html
from url
index.php?page=page&lang=en
I've tried a couple of hours and I can not get ... Unfortunately, I do not know at all mod_rewrite ...
Can someone a guiding me to a solution?
-
Peter
First, make sure mod_rewrite is enabled in your Apache configuration file.
Also, make sure htaccess files are allowed (AllowOverride All for your document root folder).
Then, put this code in your htaccess (which has to be in document root folder)
RewriteEngine On
RewriteRule ^([a-z]{2})/([^/]+)\.html$ /index.php?page=$2&lang=$1 [L]
This rule allows you to reach any language/page, for instance:
http://domain.com/en/page.html --> /index.php?page=page&lang=en
http://domain.com/fr/something.html --> /index.php?page=something&lang=fr
If you want to restrict lang parameter:
For english only
RewriteEngine On
RewriteRule ^en/([^/]+)\.html$ /index.php?page=$1&lang=en [L]
For english and french (as an example)
RewriteEngine On
RewriteRule ^(en|fr)/([^/]+)\.html$ /index.php?page=$2&lang=$1 [L]
Thank you for your help - exactly is what I meant.
Roughly already beginning to understand what's going on...
From the documentation at hand I can handle it :)
-
Peter

How to keep mod_rewrite from recognizing directories

So, lately I've been dealing with an issue relating to mod_rewrite and it seems nobody is trying to do anything like it. Every question people have is about trying to exclude directories from the rewrite, when I want them to be included like any other.
For instance, assuming my root directory with .htaccess file in it is www.example.com/root/
When I type in made up directory, such as www.example.com/root/asdfasdf, I have my .htaccess file set to redirect me to www.example.com/root/index.php?url=asdfasdf without change what's in the address bar on my browser
However, in trying to do the same with a real directory, such as www.example.com/root/admin, it not only changes the url in the address bar but changes it to www.example.com/root/admin/?url=admin.
Can anyone explain to me what's going on. I've tried all kinds of different regular expressions and flags and the ones that redirect anything still cause this same issue. can I go to www.example.com/root/admin and still get redirected to the root folder while hiding that the query string is ?url=admin.
[UPDATE: additional information 11-30-2012]
Like I said, I've tried it will multiple different lines of code and come out with the exact same redirect issue, assuming the redirect doesn't just fail altogether and produce a 500 error. Here's one of my latest iterations, though, which has produced the issue of not ignoring direcotories.
RewriteEngine On
RewriteBase /root/
RewriteCond %{REQUEST_FILENAME} !^(.\*\\.("png"|"jpg"|"gif") [NC]
RewriteRule (.\*?) index.php?url=$1 [QSA]
The rewrite condition is to keep the engine from rewriting if a picture is being requested (for css and img tags). I only didn't mention it previously because I have tried removing that line and it has made no difference.
I'm not exactly a master of mod_rewrite, though, so if you see any errors with anything I've written, please feel free to let me know.
It's not entirely clear from your question what you are trying to do and it would have been helpful to see what your .htaccess file actually looked like. However the following lines in an .htaccess file in the root folder:
RewriteCond %{REQUEST_URI} !^/root/index\.php
RewriteRule (.*) /root/index.php?url=$1 [L]
Will silently redirect requests made to http://www.example.com/root/madeupfolder/madeupfile.php to http://www.example.com/root/index.php?url=madeupfolder/madeupfile.php and will also do the same for real folders. So if the folder admin exists under root, then requests to http://www.example.com/root/admin will be silently redirected to http://www.example.com/root/index.php?url=admin
If however you wanted to serve up folders and files that actually exist, but rewrite requests for folders and files that do not exist, then you would need to adjust the rewrite like so
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/root/index\.php
RewriteRule (.*) /root/index.php?url=$1 [R=301]
This would still rewrite requests made to http://www.example.com/root/madeupfolder/madeupfile.php to http://www.example.com/root/index.php?url=madeupfolder/madeupfile.php, but for real folders and files, such as requests made to http://www.example.com/root/admin, the admin folder would be served up.
Hope this helps, but if you can clarify your question a bit then I can try and help again.

using mod_rewrite to redirect from subdomain to maindomain

My problem is that i have a functioning subdomain (sub.mydomain.com). sub is loaded in mydomain.com/sub.
What i would like to do is to redirect all requests to sub.mydomain.com to mydomain.com.
Somehow it seems that when im the subdomain i cannot access the rootfolder (main domain). I can get it working with from mydomain.com/sub to mydomain.com. But not from the subdomain.
Currently im using:
RewriteEngine on
RewriteRule ^(.*)/?$ /home/web/webuser/$1 [L]
When accessing sub.mydomain.com i get a 500 Internal Server Error.
Is there a restriction in accessing the main from a sub? (rights wise)
Or maybe another way of getting to main, perhaps something like (../$1)
Thanks
EDIT:
I only have access to .htaccess. So DocumentRoot cannot AFAIK be used in .htaccess file.
What about symlinks? I dont really know what it does, but i assume that it links two locations? The only code i found for that enables symlinks (Options +FollowSymlinks) - but this line doesnt say anything about what to link (perhaps im all wrong)
Btw. thanks for input so far !
I must admit that I did not fully understand your question. Do you want to redirect everything from sub.mydomain.com/whatever to mydomain.com/whatever? In that case, the following (put in the config file of your sub.mydomain.com) might work:
RewriteEngine On
RewriteRule ^/(.*)$ http://mydomain.com/$1 [R,L]
It redirects on the client side, meaning that the user will see mydomain.com/sub in the browser.
EDIT: I think I understand your question now. Yes, it's a permissions issue: If the DocumentRoot of your web site is /whatever/sub, then you cannot just access /whatever by adding "/.." to the URL. I hope you understand that this is a good thing. :-) mod_rewrite just changes the URL, so it cannot do that either.
So, to solve your problem, you need to either change the DocumentRoot of sub.mydomain.com or create a symlink that allows you to access the required directory (e.g. /whatever/sub/redir-target -> /whatever). Be careful with the symlink option, though, since it will create valid directories of infinite length on your file system (/whatever/sub/redir-target/sub/redir-target/...) and some scripts cannot cope with that.
EDIT2: Instead of a symlink, you might want to create an alias, e.g., something like this:
Alias /redir-target /home/web/webuser
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/redir-target/.*$
RewriteRule ^/(.*)$ /redir-target/$1
Still, I think the easiest solution is to change the DocumentRoot...
Why not try using a Redirect Directive from mod_alias?
It's difficult to provide a definitive answer without knowing more about your server configuration.
The following might work and is at the very least a decent starting point:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.mydomain\.com
RewriteRule (.*) /$1 [L]
Ideally that would go in your httpd.conf, but might work from a .htaccess file (again, more information about how your subdomains are setup would be helpful).

Resources