Send specific request uri to subdirectory using htaccess - php

I need to find a way to redirect all requests that contain the following URL:
http://local.testdomain/quality/
to use the appropriate files in {Document_Root}/quality/
ie. user requests http://local.testdomain/quality/assets/images/35.jpg
needs to get the file {public_folder}/quality/assets/images/35.jpg
What I've tried in the .htaccess file is
RewriteEngine on
RewriteCond %{HTTP_HOST} ^local.testdomain$
RewriteCond %{REQUEST_URI} !quality/
RewriteRule (.*) /quality/$1 [L]
but it doesn't seem to work.
I basically want to put a whole php project in Zend's public folder, and have that php project attainable via that request uri ({domain}/quality/)

Seems your htaccess is not correct.
Try something like :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^local.testdomain$ [NC]
RewriteCond %{REQUEST_URI} ^/quality/(.*)$
RewriteRule ^(.*)$ /PATH_TO_YOUR_PUBLIC_FOLDER/quality/$1 [L]
Let me know if it works
Best,

Related

htaccess rewrite non html request

I've migrate a dynamic .asp site to static site by using sitesucker osx app...
everything works done if you access directly!
old url:
http://www.mysite.com/content.asp?L=1&IdMen=158‎
new url:
http://www.mysite.com/content.asp-L=1&IdMen=158‎.html
i get page not found from my old referral inbound links (google, yahoo etc...)
i would like, by using .htaccess, redirect all links that not contains ".html" to permanent redirect too html page...
i've tried this but not work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.html
RewriteRule ^(.*)$ $1.html [R=301,L]
many thanks
Frankie
As per examples if you want to modify query string then you need this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} !\.html$ [NC]
RewriteRule !\.html$ %{REQUEST_URI}-%{QUERY_STRING}.html? [L,R=301,NE]

redirect to subdirectory and always get 404 not found in Slim php

I want make two slim app, one for api calling and another one for website users.
I put my main php file into 'src' directory, and the structure is as below:
/
---.htaccess
---/src(here are files)
-----.htaccess
-----index.php(Slim)
-----/Controllers
-----/Views
-----/Models
-----/api
-------.htaccess
-------index.php(Slim)
---/public
-----/js
-----/css
-----/fonts
i want to make my url from this:
http://localhost/project/src/api/foo
to
http://locahost/project/api/foo
I don't know how to write the first .htaccess(root)
i try to redirect to subdir like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ src/ [QSA,L]
but it always returns 404 Not found by Slim. i thought it did redirect to subdir but the slim router wont get correct url, am i right?
and others looks like this:(I just copy the documentation from SlimFramework)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA]
any ideas would help me a lot! thank you!
Try this rule in your /project/.htaccess:
RewriteEngine On
RewriteBase /project/
RewriteCond !/src/ [NC]
RewriteRule ^(.*)$ src/$1 [L]
Make sure this is your first rule.

.htaccess - Redirect subdomain to folder

This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.
I simply want m.daltonempire.nl to be redirected to daltonempire.nl/m/ without the user seeing the URL change.
So if m.daltonempire.nl/hello.php is requested, I want the user to keep seeing this URL, while the page given is actually daltonempire.nl/m/hello.php.
Note: I do not want www., so simply http://m.daltonempire.nl
Thanks in advance,
Isaiah v. Hunen
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/$1 [L]
The %{REQUEST_FILENAME} conditions would let you access /exists.php and not rewrite it to /m/exists.php. Remove those two if you want to rewrite even if that may potentially override existing files and directories.
Try this example:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]
Any requests http://test.example.com will be mapped to http://example.com/test/...
Try googling dynamic subdomain with php and htaccess to get better search results.
I have set CNAME of my sub domain below:
blog.mydomain.com
to my wordpress that installed in folder /blog/ under root directory.
Formerly I need to use this url to call wordpress:
http://blog.mydomain.com/blog/
which is ugly. I have tried many code to redirect:
http://blog.mydomain.com/
to the folder so I can use it as my wordpress url.
Finally I got .htaccess setting that is work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1
I have also CNAME other subdomain: http://forum.mydomain.com to mybb installation in folder /forum/mybb/ so the .htaccess need to put [L] on each of RewriteRule code as below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/forum/mybb/
RewriteRule (.*) /forum/mybb/$1 [L]
RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1 [L]
In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.

How to deal with CSS, JS files when used with global apache redirect (for user friendly url's)

I have the following in my htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
From rewrite.php I redirect to the correct pages depending on the url. Problem is that it redirects all files including css and js. I tried including these files but I now realise that was dumb of me. Should I redirect when there is and appropriate extension in the url? If redirecting is the way to go what method would be best? header location or HTTP_redirect?
Or is this not a good idea performance or work involved wise? I could go for something like this but I know next to nothing about apache and would rather not work with it right now.
RewriteRule ^(.*).css$ /includes/compressor.php?i=$1.css [L]
I previously had the following in my htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
I decided to remove this because:
I would not be able to include the header and other common files in the rewrite.php file. I would also not be able to have a database call in the rewrite file that would determine the page to include and to reuse the data for the page contents.
Unwanted files would be reachable such as service used only by external app.
The compression should be done once, and not for every request. You can then exclude requests from the URL rewriting if the corresponding file exists:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
How about redirecting only if the requested file does not exist on the server?
You could use the following rewrite conditions to achieve this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
So if the request is for a CSS/JS/HTML/Image file that already exists on the server then no rewriting takes place and the request for the file is processed. If the file does not exist, it will run your rewrite rule to rewrite.php

Prevent url with a directory from pointing to a web page

I have a domain let's call it newsite.com which is parked to maindomain.com. I use mod-rewrite to direct all requests for newsite.com to a subdirectory on maindomain.com. On newsite.com I have CodeIgniter installed. Which means the urls look like this:
newsite.com/products/shoes/
The above works just fine.
However, if I set up a controller for a page like this:
newsite.com/about/ or newsite.com/about/faqs/
I get a 404 which I believe is being caused because it is looking for the page maindomain.com/about.php which does exist, but does NOT exist on newsite.com.
My question is.... how do I PREVENT urls like newsite.com/about/ from pointing to newsite.com/about.php ? This is the opposite of what many people try to do (they tend to want to allow the file extension to be missing).
I'm wondering if it is and apache or PHP setting that causes it to look for the file first if it exists and the directory does not? Or do I need to add more mod-rewrite rules?
Thanks very much!
Edit - here is my .htaccess file currently. It resides at the web root of the maindomain.com shared hosting account.
# Redirect various urls to subfolders of the format: /site-thename/
RewriteEngine On
# Remove the www for NewSite.
RewriteCond %{HTTP_HOST} ^www.newsite.com$ [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]
# Handle pages and directories that should not go through CodeIgniter.
RewriteCond %{HTTP_HOST} ^(www\.)?newsite\.com
RewriteCond %{REQUEST_URI} !^/site-newsite/
RewriteCond %{REQUEST_URI} ^/demo/ [OR]
RewriteCond %{REQUEST_URI} ^/robots\.txt [OR]
RewriteCond %{REQUEST_URI} ^/emailform\.php [OR]
RewriteCond %{REQUEST_URI} ^/assets/ [OR]
RewriteCond %{REQUEST_URI} ^/index\.php
Rewriterule ^(.*)$ /site-newsite/$1 [L]
# Handle CodeIgniter URLs.
RewriteCond %{HTTP_HOST} ^(www\.)?newsite\.com
RewriteCond %{REQUEST_URI} !^/site-newsite/
Rewriterule ^(.*)$ /site-newsite/index.php?/$1 [L]
Asked my hosting company's support department what might be causing this... and it turns out that it is caused by Apache MultiViews. This can be turned off via
Options -MultiViews
in the htaccess file. That worked.
Thanks for the suggestions.
If you have no rewrite rules for it, Apache, by default, will reference whatever is on the filesystem. You must have rewrite rules there to tell Apache to use a specific controller to handle requests instead of attempting to reference objects on the filesystem.
I assume that if you are an apache user, you use .htaccess file for redirecting the URL to you 'index' file (let's call it index.php). Also you should have sth like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
in your .htaccess file. Note that the RewriteCond condition has a parameter which checks if it is not a file:-) More can be found at http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

Categories