.htaccess how to remove subdirectory? - .htaccess

I have a website in which I want to enter
http://mysite.com/myfolder
but go to (load index.php from)
http://mysite.com/dir/myfolder
So which kind of RewriteRule should I use?
The following rule didn't work and it caused an "500: Internal Server Error" in all pages.
RewriteRule ^$ dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ dir/$1
Thanks !

The following should work as long as no path includes the "dir" string:
RewriteEngine On
RewriteCond %{HTTP_HOST} !dir
RewriteRule ^(.*)$ http://mysite.com/dir/$1 [R=301,L]

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule (?!^dir/)^(.*)$ /dir/$1 [L,NC]

Related

Rewriting subfolders using nice url

When I go to http://www.example.com/youtube/detail/abvcde in my browser, I want to internally rewrite the url to http://www.example.com/youtube/detail.php?id=abvcde. I tried to do this with the following .htaccess, but it gives a 404 error.
Currently my .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
Put the following code in a htaccess file in server root directory:
RewriteEngine On
RewriteRule ^youtube/detail/(.+)$ youtube/detail.php?id=$1 [L]
You can put this code in your /youtube/.htaccess
Options -MultiViews
RewriteEngine On
RewriteBase /youtube/
RewriteRule ^detail/([^/]+)$ detail.php?id=$1 [L]

htaccess RewriteCond for subdirectory not working

I have my .htaccess file written as such for my main program in the top directory but also want to exclude a sub-folder where I want to run another program.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond {REQUEST_URI} !=/ecart
RewriteRule ^files/ - [L]
RewriteRule ^(.*)/$ index.php?layers=$1 [L]
</IfModule>
I am trying the RewriteCond but it's not working.
It should go to the ecart/index.php file but still I am being brought to the front domain when I put my address in as www.mydomain.com/ecart
Your code has syntax error with logical errors. {REQUEST_URI} has missing % sign and RewriteCond is applicable to next RewriteRule only. Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/ecart(/.*|)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ /index.php?layers=$1 [L,QSA]

Trouble with .htaccess mod_rewrite

Please can somebody take a look at my .htaccess code to help identify what's wrong.
I am trying to amend all example.php, example.html and example.htm URL's to index.php?filename=example.html
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(index\.php) - [L]
RewriteRule ^([^/\.]+).(php|html|htm)$ index.php?filename=$1.html&%{QUERY_STRING} [L]
</IfModule>
When I enter a URL like example.html I'm taken to the default 404 and not index.php
Try:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
</IfModule>
I'm not using the apache .hatacces for a while,
But try this one:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^.*/(css|img|js)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?filename=$1&%{QUERY_STRING} [L]
Basically what you need is to be sure that the URL didn't have already index.php. You accomplish it with RewriteCond.
After you redirect all to index.php passing the filename as a parameter.
You can add more exception to the RewriteCond like on this example, all content with /css, /js, /img will not pass to this rule.
You can check this thread: Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests also

htaccess, sub folder rewrite to index.php

how can I do this?
main link:
http://www.domain.com/?link=whatever/something/everythting
convert to:
http://www.domain.com/whatever/something/everythting
I tried with this:
RewriteEngine On
RewriteRule ^([^/]*)$ index.php?link=$1 [L]
But not working.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ /?link=$1 [L,QSA]
This should work for you:
RewriteEngine On
RewriteRule ^(.+)$ index.php?link=$1 [L]
Edit:
As #anubhava posted in his answer, you should check whether the requested file or directory exists or not in a RewriteCond.
This should work:
RewriteRule ^([^.]+)$ index.php?link=$1 [L]

How can I simplify my url with .htaccess?

I would like this:
http://www.website.com/test/view
to actually call this:
http://www.website.com/index.php/test/view
I tried an .htaccess file that looks like this but it doesn't seem to work:
RewriteEngine on
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ /index.php/$1 [L]
What am I missing?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L]
Did you set "RewriteEngine On" At The first line??
I used this once and it worked fine for me:
RewriteEngine On
RewriteRule ^([a-z\-]+)$ /index.php?page=$1 [L]

Resources