I have a directory tree that looks like:
public_html
_COMMON
INIT.php
project-1
index.php
project-2
index.php
Those two projects use files from _COMMON. First, they load some configuration via the following code in their index.php:
require_once '../_COMMON/INIT.php';
After that, they use common resources via:
<script src="../_COMMON/node_modules/jquery/dist/jquery.min.js"></script>
I have two domains. They are both connected to the same server, meaning they both serve the same files. I want to serve project-1 from:
http://example.com/project-1/
and
http://project1.com/
To do that, I rewrite project1.com URLs to project1.com/project-1/ with the following htaccess:
RewriteCond %{HTTP_HOST} ^(?:www\.)?project1.com$
RewriteCond %{REQUEST_URI} !/project-1
RewriteRule ^(.*)$ /project-1/$1 [L,NC,QSA]
Problem comes here.
The require_once part works because the URL of INIT.php is resolved after the request URL is rewritten, meaning:
User vists http://project1.com/
URL rewritten to http://project1.com/project-1/
Executing http://project1.com/project-1/index.php
http://project1.com/project-1/index.php with ../_COMMON/INIT.php resolved to http://project1.com/_COMMON/INIT.php
However, the script isn't loaded successfully. I guess that's because the URL of jquery.min.js is resolved in the browser, before the URL is rewritten. I guess the following happens:
User vists http://project1.com/
http://project1.com/project-1/index.php is served, but URL is http://project1.com/
Browser sees ../_COMMON/node_modules... and tries to resolve it
Browser initiates a request for http://project1.com/_COMMON/node_modules/jquery/dist/jquery.min.js
URL is rewritten to http://project1.com/project-1/_COMMON/... which doesn't exist
What I've tried
I could just add this:
RewriteCond %{REQUEST_URI} !/_COMMON
to my htaccess and be done.
That's kind of a cheesy solution, though. What If project-1 uses a file from another directory at the root? Adding all those directories in the htaccess seems silly. I don't need to rewrite the URL if it doesn't contain _COMMON, I need to rewrite it if it doesn't go up the directory tree, I.E. if it contains ../. Is there a way to detect that?
Basically, what I want is to not rewrite URLs that begin with ../. I tried:
RewriteCond %{REQUEST_URI} !\.\./
but it doesn't work. I guess it's because that ../ is resolved in the browser, so it's not inside the URL that is actually processed by the htaccess. Is there some other way I could detect whether the original URL contained ../?
Related
How do I point a URL to a file so when I go to the URL it points to that file but doesn't change the URL. For example:
mydomain.com/orders/create should point to /myfiles/orders-create.php
then when I go to mydomain.com/orders/create it will display all the contents of orders-create.php.
Any ideas?
If you want to do it on the server-side you could edit the .htaccess file similar to this question's answer.
The main changes you're looking at are:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*)$ $1.php [NC]
This would let you create files without an extension and it would run force run the PHP interpreter.
Here's a good read for pretty URLs to find out more ways you could do this, and it'll explain more about what they actually are.
If you can change create-order.php, then you can make it so that it does not rely on relative paths. One way to do this is to create a bootstrap.php file that has all of the includes and then your "leaf" PHP files only have to find this bootstrap.php file.
Another option is to set the PHP path. If you change the include path to include the root of the directory, then when including files in create-order.php will look in the root and it will find them. You can set the include path in your PHP files or in the PHP configuration.
If you don't want or can't change create-order.php then one way to do this would be via the webserver. For example, in apache, you can use mod_rewrite to do just that, have a public URL actually invoke a specific local file. The rewrite configuration might look like this (example for just this one file):
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^orders/create to create-order.php$ create-order.php [L]
or a catch-all rule with this (untested):
RewriteRule ^orders/(.*)$ $1?%{QUERY_STRING} [L]
I'm trying to set up the base path/file for my site on my server. The file I want to load is in my document root within the following path:
/src/pages/views/index.php
I'm trying to get my .htaccess file to point to this location by default. I have tried doing this
DirectoryIndex /src/pages/views/index.php
which does automatically route to my index file, but then all relative paths in my site no longer work. (My url bar shows just 'mywebsite.com')
If I don't have anything in my .htaccess file, then I go to mywebsite.com (which shows me the root directories) and manually click my way into the directory that I want, everything works just fine and there are no pathing issues (my url bar now shows 'mywebsite.com/src/pages/views/').
By "pathing issues", I mean all of my imported files (javascript, css, etc) are 404ing because it can't find the path.
Is there a way to actually make it so that when a user types in :
mywebsite.com
it will actually redirect them to the proper path? :
mywebsite.com/src/pages/views/
Give this a try:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/src/pages/views/$1 [R=301,L]
You can also try:
RewriteEngine on
RewriteRule ^mywebsite\.com$ /mywebsite.com/src/pages/views/?&%{QUERY_STRING}
I am unable to access my WordPress site URL, it redirects like
http://www.mydomain.net/wp-login.php?redirect_to=http%3A%2F%2Fmydomain.net%2Fmydomain.net%2Fwp-admin%2F&reauth=1
but it shoud redirect to
http://www.mydomain.net/wp-login.php?redirect_to=http%3A%2F%2Fmydomain.net%2Fwp-admin%2F&reauth=1
I believe the problem is with .htaccess file as I am rewriting the path of folder like
public_html/mydomain.net
instead of public_html as this domain is primary domain of cPanel but to avoid confusing, I created a directory and rewriting path using .htaccess.
There is nothing wrong with WordPress. In settings>general both the URLs set to mydomain.net
.htaccess code is Placed in Public_HTML
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.net$
RewriteCond %{REQUEST_URI} !mydomain.net/
RewriteRule (.*) /mydomain.net/$1 [L]
I had a similar problem after changing the root directory of my blog (before migration) and trying to coming back... Changing the data in my DB was not working for me.
I still did not figure out why but I had a "page not found" when I tried to access categories, tags,... and even on the home page. Links to articles worked correcly and the administration page was not accessible (wp-admin rewritten twice in the url).
These actions solved my problem:
Edit the wp-config.php file.
After the "define" statements (just before the comment line that says "That's all, stop editing!"), insert a new line, and type:
define('RELOCATE',true);
Save your wp-config.php file.
Open a web browser and manually point it to wp-login.php on the new server.
I had to change the value of the root directory in the administration page (after these actions gave me back the access to it).
Check http://codex.wordpress.org/Changing_The_Site_URL#Relocate_method for more info.
I just started using Amazon S3 to host static files (images, videos, etc.).
For accessing the uploaded files, temporary links are created.
A temporary link looks like this one:
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=1346888760&Signature=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D
What I want is to serve these file through my url, something like this:
http://s3.mydomain.com/zeebit/stackoverflow-logo.png/AKIAIXEHEYSBDWAAXVVA/B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D
I know I can redirect requests to http://s3.mydomain.com to the Amazon url via PHP (for example), but I don't want the address bar to change.
I can create a .htaccess to transform the url to the Amazon url, but as I know .htaccess can't redirect to external resources.
So, how can I solve this?
There are a couple of solutions:
.htaccess Solution #1 - Rewrite Rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^s3\. # Hostname starts with "s3."
RewriteCond %{REQUEST_URI} !-f # Not a file
RewriteCond %{REQUEST_URI} !-d # Not a directory
RewriteRule ^/(.+)/(.+)/(.+)/(.+)/(.+)$ http://$1.s3.amazonaws.com/$2?AWSAccessKeyId=$3&Expires=$5&Signature=$4 [R=302,L]
NOTE: Your initial desired URL was missing the "Expires" value, so the above would work for URLs formed like so:
http://s3.yourdomain.com/[[S3_Bucket_Name]]/[[S3_Filename]]/[[AWSAccessKeyId]]/[[Signature]]/[[Expires]]
So:
http://s3.mydomain.com/zeebit/stackoverflow-logo.png/AKIAIXEHEYSBDWAAXVVA/B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D/1346888760
would redirect to
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760
.htaccess Solution #2 - Redirect
Whilst being a less flexible solution than the above, you could put the following into your .htaccess file
redirect 302 /s3/ http://zeebit.s3.mydomain.com/
Using this rule, requests for
http://yourdomain.com/s3/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760
Would basically retain everything after /s3/ and simply replace everything preceeding it with the Amazon S3 location.
http://zeebit.s3.amazonaws.com/stackoverflow-logo.png?AWSAccessKeyId=AKIAIXEHEYSBDWAAXVVA&Expires=B%2BS%2FlUoRXno3UfSqf9Ua0RuCcBc%3D&Signature=1346888760
I just inherited a website built in PHP. The main page of www.mysite.com has a href to www.mysite.com/index/35.html somewhere in the page. In the site's root directory and its children there is no document 35.html.
The number 35 is actually an id found in a DB which also holds the html contents of the page.
If I load URL: www.mysite.com/index.php?id=35 the same page loads.
How does PHP know how to automatically convert
/index/35.html
to
/index.php?id=35
EDIT
Based on the answers, I have found a .htaccess file containing rewrite instructions that would explain the functionality.
However, IIS doesn't seem to (or is not configured) know how to use this. (probably because this is an Apache feature?)
So this begs the following question: Is there a way to configure IIS to work with this?
it will be done usign URL Rewriting using .htaccess - should be in the webroot.
It may look something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
May have other bits, but what this basically tells apache is to send anything that DOES NOT physically exist to index.php
It doesn't. There is a mod_rewrite rule that rewrites from /index/foo to /index.php?id=foo, either in a .htaccess file somewhere or in the httpd configuration itself.
RewriteEngine On
RewriteRule ^index/([\d]+)\.html /index.php?id=$1 [NC,L]
This is off the top of my head. Any browsers trying to load an address starting with index/ has any number ending in .html will be internally redirected to index.php?id= whatever the number is.
Edit: Just saw that your working on IIS. This probably won't work for you. Sorry.
I think you will be using .htaccess to redirect all requests to index.php. From there You can pass the query string a routing class, which will parse the url and identify the unique ids.
In this case we can say like, your routing class will parse the request /index/35.html to indexController, indexAction, id=35. now you can pass this id to the model to get corresponding page contents
NB : Here I a am assuming you are using mvc pattern. Anyway it can be treated in your own way, with the concept remaining the same. Hope this make sence.