UrlRewrite to modify relative links - php

Im running a site on MySQL/Apache/PHP and it contains a couple of pages and a top menu to help navigating between them.
In order to provide a spanish version of the site, I have created a subdirectory called /es and essentially copied the whole site and pasted it in that directory.
In a new htaccess file in this subdirectory, I would like an urlrewrite formula that modifies all relative links in this subdirectory. For example I want the link "/pages/about.html" to become "/es/pages/about.html". Essentially, I want "/es/" to be prepended to every relative url.
Is this possible? Is this a reasonable way to proceed when making new language variants of my site?

Related

PHP Includes not working after moving pages into subfolders

The problem is as follows:
Originally, all of the individual pages on my website were in the root folder of the site.
I have now moved the majority of the pages into various sub-folders.
For example: The main page for our Apache Totem Freestanding Light Box used to be in the root folder, but is now in subfolders: /lightboxes/apache-totem-freestanding-lightbox/index.php
The problem is that various elements of the page have “PHP Modules” included for common elements such as the header, the breadcrumbs, the footer, the css file etc.
For example, for the header we have as a link on each page.
The files that these “PHP includes” point to are situated in their own folder on the site root, called ‘modules’.
The trouble with this, is that the ‘PHP Includes’ can’t seem to find the modules because I guess they are expecting to see them in a folder called ‘modules’ that is situated in the same subfolder as the current page.
I’ve partially got around this by changing the link to the module to have ../../ in front to tell it that it has to look two folders up from the current folder, but the page is still having difficulties displaying all of the information.
To give you an idea of this, I have uploaded the Apache Totem Index Page in its subfolder /lightboxes/apache-totem-freestanding-lightbox/
https://www.w-co.co.uk/lightboxes/apache-totem-freestanding-lightbox/
As you can see, the main content of the page is displayed ok and the css file is doing its job with styling etc.
But there are various other problems, for example:
1. There is an image missing from the header
2. The quote button in the header and the content is pointing to the wrong place (quote.php is on the root folder but the button is trying to link to the current subfolder)
3. All of the items on the breadcrumb trail link to the current page
4. All of the links in the footer are also trying to point to the subfolder lightboxes
Is there anything you can think of to fix these issues?
Thanks,
Tony
The URL for the images should be something like this https://www.w-co.co.uk/images/worldpay/visa.gif
You have to use base URL https://www.w-co.co.uk and then image path. You can define("BASE_URL", "https://www.w-co.co.uk") and use it like in the following.
<?php
define("BASE_URL", "https://www.w-co.co.uk/")
?>
<img src="<?php echo BASE_URL;?>images/worldpay/visa.gif"

Links not working from subdirectories

I want to make multiple pages on my website, but to keep everything clean I want to make different directoriess with the different pages. However, I use php to make a different file with my header that is included in all my pages, so I only have to change the code of my header once and it will be the same on all pages.
The problem is that the links I use in my menu items (like home, contact, about, etc.) will not work anymore when you're on a page inside a directory (I'll make an example below).
So my question:
Is there a home folder on a website (like ~/ on unix) or is there another way to make it work?
Example of my directory structure:
htdocs
index.php
header.php
menus
contact.php
about.php
(a link to index.php won't work anymore if you're on the contact.php page)
Sounds like you're using relative paths in your menu links. Use an absolute path instead by starting with a "/":
Home
About
or a complete URL:
Home
About
The home directory of a website can be accessed with a simple '/' at the start of the link you want to add. From there you can enter subfolders by appending the folder name.
Example:
'example.com/subfolder/subsubfolder/page.html'

Remove folder from url slug

I have little to no experience with the following problem so I am turning to the community for help.
My Forum is installed within a folder namedforum within my root directory. When users try to login from a discussion or a thread, they get redirected to a 404 url.
The reason why they are getting redirected to 404-page is because the forum creates a second forum slug within the url, here is an example:
When Viewing a Thread
http://www.example.com/forum/discussion/1/letsTalk
The above is the correct url, HOWEVER, when user tries to login they get redirected to the following url
When Logging IN
http://www.example.com/forum/forum/discussion/1/letsTalk
Notice the forum appearing twice in the above url.
Solution
I am guessing this can be fixed within the htaccess file by creating a re-write rule, which is simple enough. However my problem comes in when users view other threads with a different url than the one given above.
Thus, I need to find a way to remove the second forum appearing within the URL which...I am not sure how to tackle? Can this be fixed within the htaccess or will source code need to be modified?
In your head section in the html you should put
<base href="http://www.example.com/forum"/>
This will make relative urls get prepended with the given base.
Then you should make another .htaccess inside the forum subdirectory
It should contain the following:
RewriteEngine On
RewriteBase /forum/
This will make relative URLs used in your backend get prepended with the base.

How to code the navigation menu for sites with subdirectories?

I would like to understand how navigation works with sites with subdirectories.
For example, if I have this layout,
\index.php
\about_us\about_us.php
The navigation link for index.php to go back to homepage would be,
Home
Whereas the one in about_us.php would be,
Home
At the moment my solution is to simply put all the files in a subdirectory so that I can easily import the menu file into all the pages for easier management. For example,
\index\index.php
\about_us\about_us.php
I can then easily import a menu file,
<?php include 'menu.php'; ?>
...into all my pages since I only need,
Home
It doesn't seem like this is a good solution (everything in a subdirectory) for creating the navigation. Would like some advise on how this is done please. :) Thank you all.
In general, it is considered best-practice to use relative URLs, so
that your website will not be bound to the base URL of where it is
currently deployed. For example, it will be able to work on localhost,
as well as on your public domain, without modifications.
-Daniel Vassallo.
However, if you are pretty sure you don't need relative URLs, you can use absolute URLs. In your case, to link to homepage from everywhere with the same anchor tag you would use:
Home

How can I get the value in the URL and modify it for paging with mod-rewrite on?

I am trying to get my old paging class to work which relied on the variables available with $_GET in PHP to reconstruct the URL for the paging links, it worked great but now I am changing to have "pretty url's" with mod-rewrite.
So this
domain.com/?p=the-pagename-identifier&userid=12&page=3
would now be
domain.com/the-pagename-identifier/12/page-3
Problem is my PHP for my old paging would rely on the GET variables to re-construct the URL and make sure any variables present in the URL remain in the new URL's it makes for new pages, now I am stuck because I need it to work with the "virtual" directories that it appears I have in the URL, domain.com/mail/inbox/page-12 is really in the root directory running through my index file domain.com/index.php?p=mail.inbox&page=12
I am lost because some pages will have more things then others in the GET part of the URL.
Since all pages are loaded through the index.php at root level I could almost just link the pages without the full URL path but instead of something like domain.com/mail/page-2 it would end up being domain.com/page-2 since the mail directory in the example is not a real directory. So is it possible to get the value in the URL of a page that was made with mod-rewrite so I can make it think it is in a subfolder and just add the page number onto the current URL of a page?
This rewrite rule:
RewriteRule ^/([0-9a-zA-Z-]+)/([0-9]+)/page-([0-9]+)$ /index.php?p=$1&userid=$2&page=$3
Would transform this:
domain.com/the-pagename-identifier/12/page-3
Into this:
domain.com/?p=the-pagename-identifier&userid=12&page=3
Completely transparent to the end user.
So aside from generating new URL's, your application need not change at all.

Categories