I'm working on the complete structure of a web page, and I'm using directories to the url of the site the user can understand the site map, with categories and subcategories. for example. My homepage is www.mantarrayamx.com.
The page I am trying to load is www.mantarrayamx.com/services/seo, but for seo I am using the subdomain seo.mantarrayamx.com to access this directory directly.
I'm using third-party code, for example "font awesome". Unfortunately, the web page loading failed because the links are relative. I try entering in the CSS and JS including of third-party code and yet it still loads with errors. You can see the difference between loading by subdomain and loading by sub-directory here:
mantarrayamx.com/servicios/posicionamientoweb/
posicionamientoweb.mantarrayamx.com/
The question is:
What is the best way to use and manage subdomains and links (../img/)?
For example: How do you do google in your applications:
drive.google.com
mail.google.com
If I have to modify the .htaccess file, please give me an example.
As far as I get your question, you are accessing a subdirectory of your server by using a subdomain. On this subdomain, your data is in the root-directory. I guess you are using absolute links in your app, like:
/service/type/(index.php) or
/about/me/(index.php)
First of all: If you just want to have this for seo-friendlieness and beautiful links, you should definitely use mod_rewrite or the appropriate nginx-config. This saves you from having real subdirectories - you just "fake" them. The following code rewrites all requested URLS to index.php?r=theenteredurl. In PHP (or, if you want, any other processing language of your choice) you can sanitize the URL, analyse it and then server the correct content.
mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule ^(.*)$ index.php?r=$1 [L]
Nginx:
location / {
try_files $uri $uri/ /index.php?r=$request_uri;
}
The good thing about this solution is, that the only file that really gets processed is your index.php and you therefor have your app/website tidy and on one place. But be aware: HTML, CSS and JS relative links do NOT work as you might expect with this solution, since they do not see what PHP processes, but only what is in the address-bar of your browser. All relative links are relative to the fake subdirectory. To solve this, you can define a base-url in your HTML-file. All other files loaded in this HTML file will be relative to this url.
If I got you wrong and you really want to have real sub-directories on the one domain and no subdirectories on the other, then you could use the HTML base-tag to define a different base-URL depending on whether you are on the main domain or the subdomain. To find out the latter, try the PHP super-global $_SERVER. Please note, that HTML cannot access something that is out of the public scope - if your ressources are in a higher subdirectory that is not publicly accessable on this subdomain, you have no chance of loading it in HTML files.
Related
So I'm trying to use url rewriting to simplify url's on a website.
Example usage:
www.example.com/test -> www.example.com/index.php?page=test
www.example.com/test/x -> www.example.com/index.php?page=test&value=x
I use this for the rewriting:
RewriteEngine on
#Simplify url
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteRule ^test/(\w+)/?$ index.php?page=test&value=$1
The issue is that this approach seems to preserve the location prior to the rewrite. So other files loaded on the website like CSS are relative to the original location rather than the location after being rewritten.
Example:
www.example.com/test/x rewrites to
www.example.com/index.php?page=test&value=x.
But CSS files loaded when a user enters www.example.com/test/x are loaded relative to the /test/ folder rather than the / folder. So they're not found.
Am I doing something incorrectly? I'd assumed that rewriting would literally redirect, so things like this wouldn't be an issue. I'd like to solve this issue rather than just using absolute url's for everything - so I can still use it on my test server.
It's important to keep in mind that rewriting is not the same as redirecting. The browser doesn't know about the rewrite that is happening; it just sees the folder structure as it seems.
Relative URLs for site resources are resolved by the browser. So, if you access www.example.com/test/x, and the browser sees <link href="style.css">, it naturally reads this as www.example.com/test/x/style.css, and tries to request this file, only to receive a 404.
One common solution is to always use absolute URLs like www.example.com/style.css. You would most likely store your site's URL as a constant and use <link href="<?php echo SITE_URL; ?>/style.css">.
1)
There is nothing wrong using absolute URLs. You can use define.
For the development environment use:
define('BASE_URL', 'http://test.server.local/');
then for the production environment just change it to:
define('BASE_URL', 'http://www.example.com/');
On all the pages of Your code, You can access those URLs as
x page
So You don't need to change code on all the pages where You reference the BASE_URL
2)
It is a good idea to place all your css in the styles/ directory, then in .htaccess file you can exclude it from rewriting like that:
RewriteEngine on
# add this line:
RewriteRule ^/?styles/.+$ - [L]
#Simplify url
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteRule ^test/(\w+)/?$ index.php?page=test&value=$1
UPDATE (regarding Your comment)
If you use the concept of BASE_URL the correct URL is made on server and then passed to the browser. If you use <base> you depend on the client side (browser the user uses). It is a good practice to use BASE_URL on the server side, thus you won't depend on the client's browser.
Check out this answer: Is it recommended to use the base html tag?
You can also include the php file (that has the define() function) to all your pages, thus there is no need to use <base> on every page. Here is a nice example of using this.
How does instagram.com pass the username variable like "instagram.com/username" or like
instagram.com/floydmayweather
without using the $_GET function and it does not turn out looking like this
instagram/index.php?username=floydmayweather
Use a URL rewrite command in your HTTP server. There are many examples out there for both Apache and nginx.
The rewrite rule happens at the server level before it hits your code. This means the URL doesn't actually have to get modified before your code receives it.
The way I do it is I configure Apache/nginx to send all URLs that do not match an existing file (so that static files like images, js and css still work) to my index.php file. Then in the index.php file I parse the URL to determine what page type to load and what data.
In your example, they would grab the last token off the URL, know that it would be a user's name in URL format, look up that user in the database and build the page accordingly.
This is where something like a front controller or URL router comes in to play in most frameworks. In index.php I would map each URL, based on its components, to a class that would then handle the actual page building.
Here is more info on the rewrite modules;
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://wiki.nginx.org/HttpRewriteModule
Some quick Googling will show you many examples for how to configure this.
Your index.php file can examine the $_SERVER array to determine the URL that has been requested. In this situation, the explode() function is your friend, for parsing the URL and checking its components :)
The Rewrite engine will be a perfect solution, for example:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Rewrite engine - A rewrite engine is software located in a Web application framework running on a Web server that modifies a web URL's appearance. This modification is called URL rewriting. Rewritten URLs (sometimes known as short, fancy URLs, search engine friendly - SEF URLs, or slugs) are used to provide shorter and more relevant-looking links to web pages. The technique adds a layer of abstraction between the files used to generate a web page and the URL that is presented to the outside world.
Usage
Instead getting URL with extenstion link (.php / html etc..)
www.stackoverflow.com/index.php
You will get URL Without extenstion
www.stackoverflow.com/index
I noticed in Drupal if you add .php to the url bar of any page it gives you a 404 message; clean urls enabled. The page is obviously a .php, but the .htaccess is preventing the user from being able to tamper with url extensions in the url bar. How could you do this using .htaccess. I have file extensions omitted at the moment, but would also like to add that feature. Thank you.
Also, this question does not pertain to Drupal. I only mentioned Drupal for and example.
Just because a file contains PHP code it doesn't mean it has to have the .php extension; even more so when you're accessing a file over the internet.
When you request http://mysite.com/page and you're using an .htaccess like Drupal's, the request is forwarded onto index.php?q=page whereupon Drupal will check it's database for a path matching page. If it finds one it will display the content for that page, if not it will (rightly) give a 404.
If you want all of your pages to be accessible with a PHP extension you could add an extra rule in your .htaccess file to remove .php from any request where the PHP file doesn't physically exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php $1 [NC]
Bear in mind though that this adds zero extra value for your site's visitors (in fact they have to remember a file extension as well as the path to the page), and it exposes exactly what server-side technology you're using so a potential attacker would have some of his work done for him.
Hope that helps.
Could you please explain that in more depth. How can it redirect content into an existing page? Is that common practice / typical way of doing things?
Yes it is a very common practice, used by most frameworks and CMS.
The principle is simple: you setup your .htaccess so that every request which doesn't match a real file or directory will be redirected to a front controller, usually the index.php in the root directory of the application. That front controller handles the request by analyzing the URL and calling the necessary actions.
In this way you can minimize the rewrite rules to just one, and you can offer customized 404 pages.
I dunno Drupal but in the usual php app every request being routed to the front controller which performs some validations and throws 404 on errors.
easy-peasy
I have a very basic two-page website: the home page, and an about page. What I'm trying to do is use htaccess to rewrite the URLs so that the appear as:
domain.com/ (for the home page)
domain.com/about (for the about page)
In the actual folder structure of the site, the homepage is /index.php and the about page is /about.php (both appear in root).
I've been doing research into using alias but unfortunately my hosting (Dreamhost) doesn't allow access to httpd.config so that's out the window and I'm left with using rewrite rules in the htaccess file.
Since the index.php file will appear in the domain root (http://domain.com/) automatically, I've so far managed to make the about page appear correctly at domain.com/about using these lines:
RewriteEngine On
RewriteRule ^about$ /about.php
I'm also using 301 redirects so that for example, domain.com/about/ (with the trailing slash) also directs back to the /about URL like this:
Redirect 301 /about/ http://domain.com/about
This works great.
However the index.php and about.php files still also show if you go to the correct URL within your browser (eg: domain.com/about.php) so as a result the search engines are seeing (and indexing) two versions of each page! I've set up the correct canonical metadata within each page but this doesn't seem to have had any effect (the canonical metadata have been within the page markup ever since the site went live).
So how would I go about firstly doing a 'blind' rewrite (for want of a more technical term) for the two files so that the URLs look correct, but also effectivly 'block' direct access to the files - ensuring that if someone were to attempt to access the php files directly, the URL would still appear in the visitor's browser as the 'pretty' versions rather than the full file name and php extension?
Any advise would be hugely appreciated, I've been researching this for another couple of days now (I don't think there's anything quite the same as this anywhere on here already) and cannot for the life of me work this one out!
Check $_SERVER['REQUEST_URI'] in the PHP files. If that contains the filename (instead of the URI that you want), then do a 301 or 404 or whatever you want. Else serve the page as usual.
You can also do a
RewriteRule ^about.php - [L,gone]
or
RewriteRule ^about.php /about [L,R=301]
but this has to go before your other RewriteRules. It will send a 410 Gone or a 301 Moved Permanently response if the page is accessed via /about.php. See Apache Module mod_rewrite for the complete documentation of mod_rewrite.
I'm using mod rewrite to redirect all requests targeting non-existent files/directories to index.php?url=*
This is surely the most common thing you do with mod_rewrite yet I have a problem:
Naturally, if the page url is "mydomain.com/blog/view/1", the browser will look for images, stylesheets and relative links in the "virtual" directory "mydomain.com/blog/view/".
Problem 1:
Is using the base tag the best solution? I see that none of the PHP frameworks out there use the base tag, though.
I'm currently having a regex replace all the relative links to point to the right path before output. Is that "okay"?
Problem 2:
It is possible that the server doesn't support mod_rewrite. However, all public files like images, stylesheets and the requests collector index.php are located in the directory /myapp/public. Normally mod_rewrite points all request to /public so it seems as if public was actually the root directory too all users.
However if there is no mod_rewrite, I then have to point the users to /public from the root directory with a header() call. That means, however that all links are broken again because suddenly all images, etc. have to be called via /public/myimage.jpg
Additional info: When there is no mod_rewrite the above request would look like this: mydomain.com/public/index.php/blog/view/1
What would be the best solutions for both problems?
Edit/Additional question:
Is there a way to make /public/ the base dir using plain htaccess code?
Write the app in such a way that it doesn't need mod_rewrite to function (at the cost of having "ugly" urls). Progressively enhance it with mod_rewrite to achieve the desired result. This probably means that you'll need to store some base path config info in your app.
I don't understand these problems at all.
Yes, this is surely the most common thing you do with mod_rewrite, yet with 2 conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
So, nothing hurt your existing images.
Why not to use just absolute path, e.g. /myapp/public/myimage.jpg, so, no virtual directory will hurt image path?
what about path info? You could use it without mod_rewrite
/index.php/path/to/another/file.jpg
<?php
echo $_SERVER["PATH_INFO"]; // outputs /path/to/another/file.jpg
?>
Anyways, if you want to know if mod_rewrite is supported by your server :
<?php
echo "mod_rewrite : ".(!empty($_SERVER["REDIRECT_URL"])?"supported":"not supported");
?>
Then you ll know if mod_rewrite is the solution or maybe path_info is more well suited for you, you could make support functions that could look for both too.