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

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/

Related

How can I create friendly url redirection made form absolute url's in .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.

How do you rewrite a URL with a query in a subdirectory using .htaccess

I'm building a website (no frameworks) and I have a .htaccess file in my root with working rewrite engine. I've been searching far and wide for a solution for my problem but nothing seems to work.
I'd like my site url to be displayed as: mysite.com/profile/profilename
and for it to be rewritten to: mysite.com/profile.php?name=profilename
so to give the illusion of there being another subdirectory when there isn't.
I've done a tonne of research and this seems to be the most logical to me but it doesn't seem to work:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^profile/(.*)$ profile.php?name=$1 [L]
(index.php is the home page to the site).
When I try this, it doesn't come up with a 404, it comes up with the profile page, but without styling, it can't access the javascript I have in the root directory folder and the query didn't execute properly.
I have some simpler rewrites already active on the site so the rewrite engine is definitely on, but this more complicated style doesn't seem to work. Is it even possible to do this? And if so, what am I doing wrong?

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).

Advanced 301 redirect for an entire site at site root?

I am struggling with an age old problem. I inherited a site with some pretty good SEO and one glaring problem. The entire site is hosted on the /site/ subdirectory. I have decided that I need to load the site at the root. So something like http://example.org/site/index.php will instead redirect to /index.php (<-- that counted as a link, if it is unclear I mean it to be the root of the site/index.php.)
We use joomla for our backend and there are hundreds of pages on the site at this point. I have struggled getting any of the redirects I have seen to do what I want them to do. Basically, any page our patrons visit from an old link with /site/ in it should be redirected to the exact same link, but without the star.
I am open to just loading the page from /site/ and making it look like it is from root. It is my understanding that this can be done with some advances mod-rewrite (http://kb.mediatemple.net/questions/85/Using+.htaccess+rewrite+rules#gs ?) but I have not had any success yet. I run a beta site that mimics the parent site in a subdomain that I have already moved from /site/ to / so I can test a lot of .htaccess configs.
Any help is appreciated... thanks!
Just to be sure: you want http://example.org/site/foo/bar/baz.php to go to http://example.org/foo/bar/baz.php, that is, to remove (via redirect) the /site prefix if it's there, but not touch the URL otherwise, right? If so, it depends on which server you're using:
If your server is Apache, you could use something like this in .htaccess:
RedirectMatch 301 ^/site/(.*)$ http://example.com/$1
If it is nginx, add this to the server {...} session of your site's file (usually symlinked inside /etc/nginx/sites-enabled):
location ~ ^/site/(.*)$ { rewrite ^/site/(.*)$ /$1 permanent; }
Here is a good explanation on how such pattern-based redirects can be set up in both servers.
This seems to be the working answer that I will go with. Basically, this needs put into the directory that you are wanting to redirect from, in my case, that was root/site/.
RewriteEngine on
RewriteCond %{http_host} !^www.beta.example.org$ [nc]
RewriteRule ^(.*)$ http://beta.example.org/$1 [r=301,nc,L]
I assume the first rule ignores www? I'd love to work around that but am not sure exactly why it would have been created anyway. this will rewrite any URL that accesses that .htaccess file (inside your subdirectory) and direct you to the same URL without the subdirectory listing. It doesn't really work with the index.php rewrite tool, but that is fine because it still reaches the correct page.
If anyone has a better option for me with use with Joomla I would be glad to hear it. But, I tihnk this is what I will go with for now because it is giving me great results.
if your server is apache2, you can configure there
<VirtualHost *:80>
ServerName sitio.com
ServerAlias www.sitio.com
DocumentRoot /home/user/public_html/sitio/
...
</VirtualHost>
You need permissions for this change

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.

Resources