Im trying to create a rewrite rule to a different directory, but unfortunatly its not working.
My current rewrite sends everything back to the index.php file unless the first word in the query string is admin. The rewrite rule for 'admin' is to admin.php, but i actually want it to go to /var/www/html/website.com/admin/admin.php (instead of the file in the same directory).
This is current .htaccess file.
RewriteEngine On
RewriteRule ^(.*\/?).*(css|images|js/)+.*$ - [L]
RewriteRule ^\/?admin\/?(.*)$ admin.php?url=$1 [QSA,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
If i change the rewrite rule for admin to use a different directory..
RewriteRule ^\/?admin\/?(.*)$ /var/www/html/website.com/admin/admin.php?url=$1 [QSA,L]
It breaks the site. Any ideas?
Thanks.
It's actually quite simple, you're using the system path, while web path is needed.
Try the following line:
RewriteRule ^\/?admin\/?(.*)$ /subfolder/admin.php?url=$1 [QSA,L]
Try these rules:
RewriteRule ^admin($|/(.*)) admin/admin.php?url=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]
Related
I've setup some rewrite rules to cleanup the URLs of my page. These rewrites work fine, except for some downloads where i don't want to rewrite the URL.
I've tried several Rules to exclude the "files" folder:
1#:
RewriteCond %{REQUEST_URI} !/files[0-9]*/
#Rewrite ulrs:
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
2#:
RewriteCond %{REQUEST_URI} !^./files
#Rewrite ulrs:
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
3#:
RewriteRule ^files - [L]
4#:
Putting a new .htaccess file in the "files" folder with
RewriteEngine Off
Are there any other methods I could try?
I do want to redirect to the pdf download i've setup without rewriting. just like:
<a href="files/myDownload.pdf">
If I quote the rewrite rule:
#RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]
It no longer rewrites the url and i can access my download, so i just need to get the folder excepted from the rule.
Do you have any Ideas or tips?
All the tries except for #2 should have worked. Please note that the order in which you put your RewriteRules is also very important.
Can you give the following a try:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/files/ [NC]
RewriteRule ^ index.php?%{QUERY_STRING} [L]
i am using codeigniter with .htaccess
in older server i had following rules in .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^transaction\.php$ index.php/printer_api/transaction/%1/%2/%3/%4/%5/%6/%7/%8 [L]
RewriteRule ^getlist\.php$ index.php/printer_api/getlist [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
it was working fine.
but after moving the website to a new server the URL .htaccess wasn't working
it was showing "Input file is not specified"
after searching the web i found a solution that is:
changing the Rule in last line from:
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
to (adding extra ? just after index.php):
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
this solved generic URL rewrite with codeigniter.
but still i have problem with following part those are specific for separate URL:
RewriteRule ^transaction\.php$ index.php/printer_api/transaction/%1/%2/%3/%4/%5/%6/%7/%8 [L]
RewriteRule ^getlist\.php$ index.php/printer_api/getlist [L]
in the first URL there are maximum 8 parameters (but the number of parameter can vary in different case
and in the second URL there is no parameter.
but these URLs are not working in new server.
now can anyone please help to make those two Rule working.
Not sure if it'll work, but did you try a similar solution as the one for index.php? Something like:
RewriteRule ^transaction\.php$ index.php?/printer_api/transaction/%1/%2/%3/%4/%5/%6/%7/%8 [L]
RewriteRule ^getlist\.php$ index.php?/printer_api/getlist [L]
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]
OK, so we were working in the wrong directory, meh!
We have a .htaccess file that is setup to redirect some files to a PHP script. Now we are adding some hard files which we want to bypass this redirect. So far this does not seem to be working and we are stumped.
Below is our initial .htaccess file contents after starting the engine which is working for another app already:
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
We are trying to load files now from /directory/ So we have tried adding each of these to the RewriteCond section:
RewriteCond %{REQUEST_URI} !directory
RewriteCond $1 !^(index\.php|i/|robots\.txt|favicon\.ico|directory)
Neither seems to be working. Any ideas on how to get this to load?
Examples:
Should redirect
http://example.com/thisredirects/
Should not redirect
http://example.com/directory
http://example.com/directory/
http://example.com/directory/index.php
etc.
have you tried making a second .htaccess file in the /directory dir and doing a NoRewrite in it?
Try this:
Wont redirect
RewriteRule directory/.* - [L]
Redirects
RewriteRule ^$ thisredirects/ [L]
RewriteRule (.*) thisredirects/$1 [L]
It seems you want to redirect anything that isn't actually an existing file in your docroot to your index.php. This little bit of rewrite should handle that:
RewriteCond %{REQUEST_FILENAME} -s [OR] # file with size
RewriteCond %{REQUEST_FILENAME} -l [OR] # file is a link
RewriteCond %{REQUEST_FILENAME} -d # file is a directory
RewriteRule ^.*$ - [NC,L] # don't rewrite [last rule]
RewriteRule ^(.*)$ /index.php/$1 [NC,L] # everything else gets sent to index.php
The [OR] operator may be what you are looking for on the RewriteCond as well.
Also if you just want to whitelist the /directory portion you could put a Rule before your redirects that is marked [L] for "last rule"
RewriteRule ^/directory.*$ - [NC,L]
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]