Alias, mod_rewrite or something else for shared php scripts? - php

I have a number of domain names all pointing to the same server and I'm looking for a way to access a set of scripts in a single location from each domain name as a kind of shared resource.
e.g.
http://abcdeals.com/shared-resource/
http://defdeals.com/shared-resource/
http://hijdeals.com/shared-resource/
all use the same files at /home/zdeals/public_html/shared-resource/
Full details:
The "shared resource" files reside in the following server path
/home/zdeals/public_html/shared-resource/
The multiple domain names e.g. (abcdeals.com, defdeals.com and hijdeals.com) reside in the following server path
/home/abcdeals/public_html/
/home/defdeals/public_html/
/home/hijdeals/public_html/
Ideally I would like to be able to access the shared resource files without redirecting the user by accessing
http://abcdeals.com/shared-resource/
http://defdeals.com/shared-resource/
http://hijdeals.com/shared-resource/
I've been looking at alias and mod_rewrite but to no avail.
Some things I've tried in /home/abcdeals/public_html/.htaccess
AliasMatch ^/shared-resource(.*) /home/zdeals/public_html/shared-resource$1
or
RewriteRule ^shared-resource/(.*) home/zdeals/public_html/shared-resource/$1 [L]
...and many more similar
I either get a 500 internal server error or 404 not found error when accessing http://abcdeals.com/shared-resource/
I also have access to the httpd.conf file if that helps.
The existing .htaccess file is as follows
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
#RewriteBase /
RewriteRule . index.php [PT]
RewriteRule ^(system/(classes|locale|schema|$)) index.php [PT]
I will be eternally grateful for any help on this.

You can use symlinks for this. In linux you can do this by:
ln -s /home/zdeals/public_html/shared-resource/ /home/abcdeals/public_html/shared-resource/
Which creates a symlink /home/abcdeals/public_html/shared-resource/-->/home/zdeals/public_html/shared-resource/

Related

Using .htaccess to rewrite file path to file name with a query string

I'm having an issue with rewriting a file path to a file with a query string
I'm trying to take a url that looks like this:
https:url.com/products/1_Samsung_Galaxy
and rewrite the request to my server like this:
https:url.com/model.php?id=1&title=Samsung_Galaxy
Here's what my htaccess looks like so far:
RewriteEngine On
#rewrite no ending files to .php if file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !products/
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/products/([1-9]+)(.*)$ model.php?id=$1&title=$2 [NC,L]
with the current config, the request is going to a file in the same directory as model.php called products.php, but seemingly in a different directory because some of the script files referenced in products.php with relative paths are throwing errors that the objects they contain do not exist.
Really my main issue is that I don't know apache syntax or how it works and I'm having trouble find a resource where I can learn about apache. The apache docs aren't much help for some reason. If there's a good resource to learn about how to write apache files, I'd love to check it out too.
Your question is a bit vague, but I think this is what you are looking for:
RewriteEngine On
# rewrite /products/<id><slug> to /model.php/.....
RewriteRule ^/?products/(\d+)(.*)$ /model.php?id=$1&title=$2 [L]
# rewrite to "...php" if such file exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
These rules will work if implemented in the http server's host configuration. Or, if you do not have access to that, in a distributed configuration file (".htaccess") located in the DOCUMENT_ROOT folder of the http host.
The main differences to your attempt:
The target paths are written as absolute path. This solves the issue with requests getting rewritten to different folders.
The exception rewriting to /model.php is implemented first, so higher up in the file, since the rules are processed from top to bottom. That saves some conditions.
You also mention that you are "not familiar with apache syntax" and have difficulties finding resources to learn this.
I would always recommend the official documentation of the apache modules. They are of excellent quality and come with great examples:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html

Send all Request to Inside folder in php?

sharedHostingFolder
folder1
folder2
folder3
xampp htdocs folder
Folder Structure:
root/sharedHostingFolder
-need an .htaccess file here
-folder1
--index.php
--dude.php
-folder2
--hello.php
--index.php
---folder5
----index.php
-folder3
--hi.php
--index.php
Request Structure
Request "root/sharedHostingFolder/hello.php" will show file inside "root/sharedHostingFolder/folder2/hello.php"
Request "root/sharedHostingFolder/" will show file inside "root/sharedHostingFolder/folder2/index.php"
Request "root/sharedHostingFolder/folder1/dude.php" will show file inside "root/sharedHostingFolder/folder1/dude.php"
Request "root/sharedHostingFolder/folder1/" will show file inside "root/sharedHostingFolder/folder1/index.php"
Request "root/sharedHostingFolder/folder2/hello.php" will show file inside "root/sharedHostingFolder/folder2/hello.php"
Request "root/sharedHostingFolder/folder2/" will show file inside "root/sharedHostingFolder/folder2/index.php"
Request "root/sharedHostingFolder/folder3/hi.php" will show file inside "root/sharedHostingFolder/folder3/hi.php"
Request "root/sharedHostingFolder/folder5/" will show file inside "root/sharedHostingFolder/folder2/folder5/index.php"
I HAVE NO ACCESS TO php.ini file
.htaccess file
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_URI} ^/folder1/ [OR]
RewriteCond %{REQUEST_URI} ^/folder2/ [OR]
RewriteCond %{REQUEST_URI} ^/folder3/
RewriteRule - [END]
RewriteRule ^ /folder2%{REQUEST_URI} [END]
httpd.conf file:
<Directory />
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Error:
object not found
I researched and found out .htaccess will do that easily . But, Really I don't know how to do that.
This is a strange question. TO me it reads a bit like a classical xy problem... As if you did not ask how to solve your actual task, but how to get something to work that you think might solve your task...
Anyway, looking at your examples I have the impression that little is to be done, mostly some exceptions that need to be implemented as rewriting rules. So something like that:
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder1/ [OR]
RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder2/ [OR]
RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder3/
RewriteRule - [END]
RewriteRule ^ /sharedHostingFolder/folder2%{REQUEST_URI} [END]
This leaves requests to URIs starting with one of the folder names untouched, replying on classical file system mapping and defining the index.php script as fallback logic for those folders. And it maps everything else into folder2.
It does not get clear from your question what that "www" string is meant to represent. If that is meant to represent another folder level then you might have to add that level to the rules above.
For this to work the rewriting module needs to be enabled, obviously. Best is to implement such rules in the http server's host configuration. If you do not have access to that you can also use a distributed configuration file (".htaccess"), but you need to enable the interpretation of such file for the requested location (see the apache documentation about the AllowOverride directive).

User-Friendly Redirect If File Not Exist

I feel like this is a rather common request, but I am too confused about .htaccess and couldn't find a solution by Google.
I have a Laravel instance in a subdirectory of the Apache2 htdocs. Now I would like to invisibly redirect all requests from the root domain to this folder (it should be the "root" website). But the tricky thing is, this is not the only folder, there are other folders directly in the htdocs, which should be reached normally. Just the "root" website is not in the root but also in a subfolder. For example:
https://domainA.com should load https://domainA.com/laravel/public (including possible query string or parameters, but invisibly for the user)
https://domainA.com/websiteB should be served as it is
https://domainA.com/websiteC should be served as it is
...
I assume, part of this solution will be to list all the websiteB, websiteC directories in the .htaccess, would it be possible to automate this?
Thanks in advance!
You can put a .htaccess in the folder you want to custom controle but you have to create some filter condition
<IfModule mod_rewrite.c>
RewriteEngine On
## RewriteBase /foo
## conditions to tell what to redirect ie on URI
## RewriteCond %{REQUEST_URI} ^/a-folder/
## not websiteB or websiteC
RewriteCond %{REQUEST_URI} !^/websiteB/
RewriteCond %{REQUEST_URI} !^/websiteC/
## if the file does not exist call index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ my/path/to/a/script.php [L]
</IfModule>
After you have to do something special in script.php for those HTTP calls
You can also rewrite the URI and pass it again to apache but things can be complicated after...

localhost vs. remote server htaccess values

I have set up Usbserver as my local webserver on my PC so that the server root is http://localhost:8081/ but all my webprojects are in http://localhost:8081/WWW/PHP/ subdirectory of my local server.
Now: all is working fine - all relative paths work as they should but as I play with the .htaccess file and I want to set up my custom 404.php page with relative path like this:
ErrorDocument 404 /php/errors/404.php
...it just do not work as expected cos it, of course, points to http://localhost:8081/ instead of, let's say, http://localhost:8081/WWW/PHP/myproject/ that is my actually acting kind-of server root (cos once uploaded to remote server it would become http://myproject.com/).
This would be absolutely fine on remote server but not here on my local server structure. I also try one suggestion like this:
ErrorDocument 404 ./php/errors/404.php
...but it ended up with blank white page with sentence "./php/errors/404.php" displayed.
QUESTION: how to write the code in .htaccess "universally", so it would think that root server is in "myproject" directory instead of "http://localhost:8081/" but once uploaded on my remote server, let's say http://myproject.com/ it would still work as normally?
REMEMBER: I cannot use rewriting the root of my local server in Usbserver settings cos I have also quantum of other webpages in "http://localhost:8081/WWW/PHP" so they all would stop working that way!
I guess there is some kind of RegEx code that would solve this (as I saw many times here on Stackoverflow guys do this kind of stuff that way) but I am quite bad in RegEx myself so without help from others I am "lost".
I searched web already before asking this here: there were many solutions but I did not find any that would actually solve this specific task (most of them suggesting just rewriting server root in my Virtual Server - Usbserver - settings which I cannot do for reason explained above).
I simply need it to work OK on localhost and on remote server afterwards without rewriting my .htaccess code everytime I upload my website to remote server and vice versa.
P.S.: I also noticed that when I set up my custom 404.php page with absolutle path for my local server like:
ErrorDocument 404 http://localhost:8081/WWW/_PHP_/myproject/php/errors/404.php
...it works BUT it change URL to that custom 404 page instead of staying at the original "not existent" URL address - why? Cos with normal not-edited 404 page the URL doe snot change, it stays the same (= that non existent link).
EDIT - FINAL SOLUTION:
So after hours of playing, browsing web, searching for any bits of some additional useful information I finally got it working exactly as I wanted - different directory for error pages (404 etc.) specified in case of localhost and remote server, here it is (mind you: it is my own code so it may not look good - I am newbie in .htaccess stuff - but at least it really work for me as I expected it to work):
# RULES FOR ERROR PAGES DIRECTORY IN CASE OF localhost:8081
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^localhost:8081 [NC]
RewriteRule .? /WWW/_PHP_/myproject/php/errors/404.php [L]
# RULES FOR ERROR PAGES DIRECTORY IN CASE OF remote server (actual webpage on public server)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^localhost:8081 [NC]
RewriteRule .? /php/errors/404.php [L]
Relative path is not allowed in error document directive. so you can use an absolute path instead of the full url.
ErrorDocument 404 /www/_php_/myproject/php/errors/404.php
Reference :
https://httpd.apache.org/docs/2.4/custom-error.html
This is the working solution
# RULES FOR ERROR PAGES DIRECTORY IN CASE OF localhost:8081
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^localhost:8081 [NC]
RewriteRule .? /WWW/_PHP_/myproject/php/errors/404.php [L]
# RULES FOR ERROR PAGES DIRECTORY IN CASE OF remote server (actual webpage on public server)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^localhost:8081 [NC]
RewriteRule .? /php/errors/404.php [L]

The requested URL /how-do-i-find-the-difference-between-two-dates-using-jquery was not found on this server

I have sub-domain likedemos.testing.com and i would like to get my url like
demos.testing.com/how-do-i-find-the-difference-between-two-dates-using-jquery
I have index.php file in demos.testing.com root folder and i reading the first parameter as
$request_uri = substr($_SERVER["REQUEST_URI"], 1);
When i request site like : demos.testing.com/how-do-i-find-the-difference-between-two-dates-using-jquery it is giving following error :
The requested URL /how-do-i-find-the-difference-between-two-dates-using-jquery was not found on this server.
Please tell me how can rewrite url in .htaccess file.
.htaccess is an Apache specific configuration file, and more over depending on your main Apache configuration, it may or may not even be parsed. To get an answer, you need to properly tell us what Web server you are using (is it Apache? If not .htaccess won't work and you need to write your rewrite rules in some other Web-server specific way). If Apache, do you have AllowOverride set?
Assuming all of this is true (you use Apache and it is set to read .htaccess), try this rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
This basically rewrites everything which is not a real existing file or directory to index.php.
As I said, depending on your setup, this may not work. If it doesn't, please tell us more about your setup (which server, what PHP SAPI, etc.).

Categories