Rewrite URL to get contents from sub-directory - .htaccess

I have edited my /etc/hosts file to link domain.com to my localhost. Now when I visited domain.com I want it to get the site contents from domain.com/site/ but only using the URL domain.com thus removing site/ from the URL.
I have done this once before, so I know it's possible, but I lost the code. Can someone please assist me? .htaccess scripting is not my strong. Apologies if this is a duplicate of something else.

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^((?!site/).*)$ site/$1 [L,NC]

I have managed to get this working with the following code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)your_directory
RewriteRule ^(.*)$ your_directory/$1 [L]

Related

What is wrong htaccess

I have the following htaccess rules but I dont know how to make it work:
RewriteEngine on
RewriteRule ^game/([^/]*)$ index.php?cash=$1
The following is the screen shot of my folders structure:
Can someone tell me how to get it to work? I have also tried:
RewriteRule ^index.php([^/]*)$ index.php?cash=$1
The URL that I want to display is: http://localhost/biteep/game/100 while the URL that I want the browser to go to is http://localhost/biteep/game?cash=100
Try to put a rewrite base into your .htaccess:
RewriteBase /biteep/
And this route should be enough:
RewriteRule ^game\/(\d+)$ index.php?cash=$1
So it works when I do this
RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?cash=$1

.htaccess - Rewrite https://www.sub.domain.com to https://sub.domain.com

I got a .htaccess issue, and I don't know why, because it should be working.
What I'm trying to do with this code is redirect https://www.sub.domain.com to https://sub.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://sub.domain.com/$1 [L,R=301]
When I test this code at http://htaccess.madewithlove.be/, it works succesfully.
(Request url: https://www.sub.domain.com, output url: https://sub.domain.com)
Also as mentioned in this question, I checked my DNS records , and there is an www.sub which points to the some domain as sub (both A's).
At this point I'm stuck, does anyone know what I did wrong?
Any help would be appreciated.
Create a file called .htaccess in the doc root (web / httpdocs / www folder) of https://www.sub.domain.com and put in the code below
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://sub.domain.com/$1 [R=301,L]

Redirecting Subdomain to Subfolder

I try to add the following logic to a .htaccess file:
subdomain.domain.xx make a redirect to subdomain.domain.xx/subdomain/
xy.domain.xy redirect to xy.domain.xy/xy/ (it should work with every subdomain without adding new rewrites)
i found a lot of solutions to redirect subdomain.domain.xx -> domain.xx/subdomain, but nothing like i need... below you find the code i try out.
Hope someone can help me. Thanks.
RewriteEngine on
RewriteBase /
RewriteRule ^([^.?]+[^.?/])$ $1/ [R]
RewriteCond %{HTTP_HOST} ^(www)?.domain.ch/$
RewriteRule .* http\://%1$1.domain.ch/%1$1 [I,R=301]
Place this in the .htaccess file of your subdomain's root directory:
DirectoryIndex index.html
RewriteEngine on
Redirect permanent /index.html http://subdomain.domain.xx/subdomain/

Using .htaccess and mod_rewrite

I'm having some mod_rewrite problems with my .htaccess..
RewriteBase /folder
RewriteEngine on
Rewrit­eRule ^(.*)$ basic.p­hp?­url=$1 [L]
Above is what I'm currently using. However, I have no idea what I'm doing to be honest as I'm just cycling through the internet trying to figure this out.
Basically, for my website, if you type in
www.domain.com/folder/xxx/
I want it to basically be www.domain.com/folder/basic.php?url=xxx.
For some reason, all that does is cause a 404 error :/
So can someone please politely point me in the right direction?
Ok, I will explain using your htaccess.
RewriteEngine on
Turn on the Rewrite Module to enable URL rewriting
RewriteBase /folder
Rewrite the Base Directory to directory name folder
RewriteRule ^folder/(.*)$ basic.php?url=$1 [L]
Remap every request make using the folder/***** to basic.php?url=********
Note: RewriteEngine On Should be the first statement on your case
RewriteEngine on
RewriteBase /
RewriteRule ^folder/(.*)$ folder/basic.php?url=$1 [L]
This is more of a problem with regexs than .htaccess files.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://www.regular-expressions.info/

localhost htaccess url rewrite problem

I have already configured my apache(mod_rewrite,alias,allowOverride), and I am trying to rewrite
http://localhost/b33m/view_blog_details.php?post_id=4
To
http://localhost/b33m/blogs/4
so that user can simply type in the second url and it would work. But I am getting a 404 not found error.
I am using WAMP on XP, and my .htaccess is in www->b33m
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^b33m/blogs/([^/]*)$ /b33m/view_blog_details.php?post_id=$1 [L]
Try:
RewriteEngine On
RewriteRule ^blogs/(\d+)$ view_blog_details.php?post_id=$1 [L]
Note the / in the beginning of each URL have been omitted.
Try the following:
EDIT: I updated the code which hopefully will work better
RewriteEngine On
RewriteBase /b33m
RewriteRule ^blogs/([0-9]+)$ /view_blog_details.php?post_id=$1 [L]
Since you expect an id I'd rather use:
Moreover shouldn't you put a trailing slash?
RewriteEngine On
RewriteRule ^/b33m/blogs/(\d+)$ /b33m/view_blog_details.php?post_id=$1 [L]
Try adding this in your hosts file:
www.localhost.com 127.0.0.1
.htaccess sometimes doesn't like localhost

Resources