How can I create friendly url redirection made form absolute url's in .htaccess? - .htaccess

I'm writing a program that creates custom friendly urls for websites in .htaccess file. The problem is that I want to create redirect from absolute friendly url to another absolute "real" url, because .htaccess file is placed in root directory and websites are placed in many different child directories.
If the pages were placed in the same folder, I could do something like this:
RewriteEngine On
RewriteRule site site.php
RewriteRule site-two site-two.php
but the pages are placed in different child folders, so the above code doesn't work.
I tried to do something like this but it doesn't work:
RewriteEngine On
RewriteRule http://www.randomdomain.com/sites-dir/site/ http://www.randomdomain.com/content/sites-dir/site.php
RewriteRule http://www.randomdomain.com/sites-dir/site-two/ http://www.randomdomain.com/content/sites-dir-two/site-two.php
I would not like the solution to be similar to 301 redirect because with it, .htaccess refers to another page, but does not save the friendly address in the url bar.
I also want linking pages using a friendly url to be possible.
Does anyone know the solution to this problem?
Thank you in advance for your answer.

Related

Htaccess and redirect all content of folder, but exlude the main folder

I got the following urls:
domain.com/categoryA/articleA
domain.com/categoryA/articleB
I want to redirect:
domain.com/categoryA/articleA -> domain.com/categoryB/articleA
domain.com/categoryA/articleB -> domain.com/categoryB/articleB
but leave it as it is and do not redirect the main folder: domain.com/categoryA/
I tried to use the rule:
RewriteRule ^categoryA/(.*)$ /categoryB/$1 [R=301,NC,L]
but it also redirect domain.com/categoryA/ to domain.com/categoryB/
How to exclude from the above rewrite rule the redirection of the main folder (categoryA), but still redirect all that is in the folder (and then change also the root folder)?
I am looking for a solution that is SEO friendly (I got the same articles in two categories, but want still to have indexed domain.com/categoryA, but the rest only as domain.com/categoryB/xxx.
Best Greetings,
Mat
With your shown samples/attempts, please try following Rules in your .htaccess file. Please make sure to place this rule under your domain redirect rule(if its there), also make sure to clear your browser cache before testing your URLs.
RewriteRule ^categoryA/([\w-]+)/?$ /categoryB/$1 [R=301,NC,L]

Implementing 301 redirects in sub-folder of Silverstripe

I am trying to create 301 redirects from a silverstripe subdirectory to a new site/subdomain on different server, can't seem to make it work!
So need www.researchnutrition.com.au/practitioner redirecting to www.practitioner.researchnutrition.com.au
along with a number of other urls within the practitioner subfolder specified to new pages on new domain.
Have tried several combinations or rewrite/redirect rules but nothing seems to be taking effect.
Also should the .htaccess file where I make the changes need to sit within the root folder or within the /practitioner folder.
Thanks so much in advance.
You can use .htaccess file in the practitioner folder:
RewriteEngine on
RewriteRule (.*) http://www.practitioner.researchnutrition.com.au/$1 [R=301,L]
Another option is just creating a redirection page in the cms and redirecting that to an external url (which is, despite the option saying "redirect to internal page", possible).

Website pages with slashes

Just building a site using php. I would like to know how to create pages without having to make ".php" links for every page. For example I want to build the site to be as follows: www.mysite.com/products instead of www.mysite.com/products.php. (Further example www.mysite.com/products/headphones )
An example of what I am looking for is as follows: http://www.starbucks.ca/store-locator
Any idea how starbucks is doing this? It isn't making a separate folder and index for every page is it?
Thanks in advance!
Jason
You can use URL rewriting to do this. You can create a .htaccess file, place it in the root directory of your website with the following content (as an example):
RewriteEngine On
RewriteRule ^tutorial/([0-9]+)/$ index.php?tutorial=$1
RewriteRule ^page/store-locator/$ index.php?page=store-locator
RewriteRule ^$ index.php
These are some basic rules that tell the web server to rewrite a URL without .php to a URL that does have .php.
In the above example, when you are visiting example.com/tutorial/3/, it will actually visit example.com/index.php?tutorial=3 and then your index.php will display the contents. It's similar for the store-locator page.

.htaccess rewriting - URLs with multi level directories

I have a page deals.php which is placed at root directory that is being accessed by number of internal URLs with multi directory levels. for example this page would be accessed by both following urls.
http://domain/flights/asia/bangkok
http://domain/flights/bangkok
I am using this code in .htaccess to redirect to deals.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^flights/asia/([^/]+)/?$ deals.php?country=$1 [NC]
RewriteRule ^flights/([^/]+)/?$ deals.php?country=$1 [NC]
Now the problem here coming is that on deals.php all the images, script and style sheet files are not opening properly. If I try to fix it against one URL by placing ../../ in addresses of all images, script and css, it dont work for other URL.
How to solve this problem? what are the options?
Easy: DO NOT use ../ in links to any resources (images/css/js/etc) -- always use URL that is relative to the WEBSITE ROOT -- that is a "requirement" when you dealing with nice/not-real/rewritten URLs as such URL rarely points to the physical file location.
Lets assume you have a logo that is located at http://www.example.com/images/logo.png.
Now, instead of ../images/logo.png and/or ../../images/logo.png ALWAYS use /images/logo.png.
i think what you want is placing an baseurl in html page, so all the relative path will related to the baseurl, not the current URL in the navigator bar

.htaccess to redirect "/whatever" links to a subdirectory

I coded a website assuming the test server would be something like (http://test.domain.com), however, it ended up being at http://www.domain.com/test. I don't have access to the hosting panel to create a subdomain, whatsoever.
The .htaccess file has to reside in /test/ and all links such as href="/css/style.css" should load from "/test/css/style.css.
Is this possible?
if you do something like this..
RewriteRule ^css/style.css$ /test/css/style.css [L]
as you can see, you can only redirect from that directory (where the .htaccess is at) and under, not above
so, i don't think it'll work, because you need access to / and not just /test/

Resources