How to forward root request to another directory with htaccess? - php

I have a root directory which has only test index.php file and .htaccess. Can you please help with forwarding root request (example.com) to child directory (example.com/wp/index.php). I don't need any redirects, because they will change the URL in the address bar.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule $^ /wp/index.php [L]
</IfModule>
It is my .htaccess file in root directory for now. And with that .htaccess, server just return me /index.php (root index, not from sub directory).
So I don't think that it is very hard question, but I can't find where I'm making mistakes.

RewriteRule $^ /wp/index.php [L]
You have the regex anchors (end of string $ and start of string ^) round the wrong way. However, you could also have issues if you have a DirectoryIndex set (mod_dir will trigger an internal subrequest for /index.php, so the URL-path is not necessarily empty).
Try something like the following instead:
RewriteEngine On
RewriteRule ^(index\.php)?$ /wp/index.php [L]
The RewriteRule pattern ^(index\.php)?$ matches either an empty URL-path, or index.php (the result of the mod_dir subrequest).
No need for the <IfModule> container and RewriteBase is not being used here.

Related

.htaccess file is not working in my Server

My site is located in https://itjmovies.com/milan/public/ and I want to rewrite the URL by .htaccess file. From https://itjmovies.com/milan/public/ To https://itjmovies.com/milan/ but it is not working.
And also https://itjmovies.com/milan/public/auth/index.php?page=new To https://itjmovies.com/milan/public/auth/new/ but this is also not working.
I have kept my .httaccess file in /www/wwwroot/itjmovies.com/milan/.htaccess
My .htaccess file:
RewriteEngine On
RewriteRule ^/(.*)$ /public/$1 [R=301,NC,L]
RewriteRule ^public/auth/([a-zA-Z]+) /index.php?page=$1 [QSA,L]
Thank You :)
I'm not sure that your edit made your question any clearer. In your question description the stated rewrites (from/to) appear to be the wrong way round to what I think they are intended (and conflict with the order in which you have written the directives), but anyway...
My assumptions:
/public should not be part of the visible URL. Although your site is located in the /milan/public directory. You are making requests of the form /milan/<anything>.
You need to internally rewrite all requests from /milan/<anything> /to /milan/public/<anything>.
Requests of the form /milan/public/auth/<something>/ (note the trailing slash, as stated in your example) should be internally rewritten to /milan/public/auth/index.php?page=<something>
I would have 2 .htaccess files. One in the /milan subdirectory that simply rewrites/forwards requests to the public subdirectory. And another .htaccess file in /milan/public that handles rewrites that are specific to your application.
For example:
# /milan/.htaccess
RewriteEngine On
# Forward all requests to the "public" subdirectory
RewriteRule (.*) public/$1 [L]
# /milan/public/.htaccess
RewriteEngine On
# Rewrite "auth/<something>/" to "auth/index.php?page=<something>"
RewriteRule ^auth/([^/]+)/$ auth/index.php?page=$1 [QSA,L]
The .htaccess file at /milan/public/.htaccess also serves to prevent a rewrite loop when requests are rewritten to the public subdirectory by the .htaccess file in the parent directory. This is because mod_rewrite directives are not inherited by default.
The QSA flag is only required if you are expecting query strings on the original request.
The RewriteRule pattern (1st argument) matches the URL-path relative to the directory that contains the .htaccess file.
RewriteRule ^/(.*)$ /public/$1 [R=301,NC,L]
RewriteRule ^public/auth/([a-zA-Z]+) /index.php?page=$1 [QSA,L]
A few notes on your attempt - which is close, but has a few crictical errors:
The URL-path matched by the RewriteRule pattern does not start with a slash (when used in .htaccess), so the regex ^/(.*)$ will never match.
The first rule is also an external redirect (ie. exposes the /public subdirectory) which doesn't seem right. Do you really want /public in the visible URL - if so then you should be linking directly to the /public subdirectory, not relying on a redirect?
The first rule is redirecting to /public in the document root, not /milan/public.
Once corrected, the first rule will also result in a rewrite-loop (500 Internal Server Error) as it will repeatedly rewrite the request... public/public/public/<something> etc.
The second rule is also rewriting to /index.php in the document root, not /milan/public/auth/index.php.

url rewriting with .htaccess

I'm trying to rewrite the url on a site I have on localhost (port 8000), I have already set this up for certain other directorys but I can't manage to do it for this one. I compared it to several other working .htaccess files but there wasn't any difference. (The module is activated.) The site is in a subdirectory called HTF and the file single.php works just fine. The htaccess file is placed in the same directory.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule videos/([0-9]+)/$ /HTF/single.php?id=$1 [L]
I would like to get an url like localhost:8000/HTF/video/1232434 for exemple.
Any help appreciated, tested several types of htacces files, here a working exemple for a other subdirectory called sorted:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^image-post/([0-9]+)/(.+)\.php$ /sorted/image-post.php? id=$1&title=$2 [L]
RewriteRule ^pages/([0-9]+)/(.+)\.php$ /sorted/page.php?page_number=$1&cat=$2 [L]
Your rule will match videos, not video as you were wanting. Also it will only match with a trailing slash.
This will work
RewriteEngine on
RewriteRule ^video/([0-9]+)/?$ single.php?id=$1
localhost:8000/HTF/video/1232434
will not match
videos/([0-9]+)/$
because of the trailing slash /. Try /? to make the slash optional or leave away. And videos.
^/HTF/video/([0-9]+)/?$ /HTF/single.php?id=$1 [L]
or
^/HTF/video/([0-9]+)$ /HTF/single.php?id=$1 [L]
This shall do the job. Sorry can not try here.

Mod_Rewrite: Redirect all reqs to subdirectory so subsequent redirects don't require the subdirectory

I'm redirecting my root to a subdirectory on my server:
RewriteEngine On
RewriteRule ^(.*)$ /_from_git/$1 [L]
In that subdirectory (_from_git/) will also be .htaccess files with redirects. I want my like below redirects to be exact matches using the start (^) and end ($) selectors, but I don't want to have to include ^_from_git/ in every rewrite.
So my question is - How can I modify my root redirect above, so my subsequent redirects can be
like this:
^old_page\.html$ /new_page.html [R=301,L]
and not like this:
^_from_git/old_page\.html$ /new_page.html [R=301,L]
Thanks!
edit: Here you can see the location of my .htaccess files:
Root .htaccess:
Subdirectory's .htaccess:
If you create a /_from_git/.htaccess file then you can have these rules without the need to prefix each rule pattern with /_from_git/:
RewriteEngine On
RewriteRule ^old_page\.html$ /new_page.html [R=301,L,NC]

php mod rewrite .htaccess ignore directory

What rule should i set, to make the mod_rewrite ignore the directory "public" completely?
By that, I mean, the files should be accessible within it, but if the file does not exist, a server error page should come up with something like, FORBIDDEN, or FILE NOT FOUND what ever. I do not need custom error pages or stuff like that. I simply want the "public" to behave like there is no mod_rewrite at all.
Here is my .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
My file structure is
/system/
/application/
/public/
I want the folder public to behave, like there are no rewrite rules set at all, completely ignore it.
edit
That's my .htaccess file:
RewriteEngine on
RewriteRule ^(public)($|/) - [L,NC]
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I already had this .htaccess in the /public/ folder:
RewriteEngine off
I've tried all the different answers above (and a ton from google). I've tried to mix 'em up what so ever.
My folders:
/system/
/application/
/public/
/public/.htaccess #RewriteEngine off
/public/favicon.ico
/index.php
Below are the url with results I'm getting:
/public/favicon.ico -> I get the favicon
/public/faviDon.ico -> I get the index.php (without mod rewrite you would get "not found")
/public/ -> I get the index.php (without mod rewrite "forbidden")
So it still does rewrite urls, if the file was not found, or upon accessing a folder directly.
Can you se it?
Thank you very much for effort guys! I really appreciate it!
EDIT
I completely setup your files on my machine
// /.htaccess
RewriteEngine on
RewriteRule ^(public)($|/) - [L,NC]
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
.htaccess in the public folder:
// /public/.htaccess
Options -Indexes
RewriteEngine off
This disables rewriting like you wanted.
/public/ -> 403 Forbidden
/public/favicon.ico -> 200 File found
/public/not-existing.ext -> 404 File not found
Do you have a index.php in you public folder?
Maybe you could remove that one..
What kind of machine your testing on?
I tested it on Linux + Apache 2 + PHP5.3
I can give you more support in the afternoon (my time +2 GMT)
EDIT 2
When I remove this line from /.htaccess is still works
RewriteRule ^(public)($|/) - [L,NC]
Everything is handled by the .htaccess in the public folder.
Maybe it's a caching problem in your browser. Try a different browser/clean up history/install app to remove cache.. (depending on what browser you're using)

URL rewriting-page not found error

In order to convert my dynamic URL i.e www.3idiots.co.in/index.php to static url i.e www.3idiots.co.in/index.html, I edited my .htccess file and put the following code in it:
RewriteEngine On
RewriteRule ^index.php$ /index.html [R]
when i uploaded this file in the root directory,and try to open the page, I got the error
404 page not found error, www.3idiots.co.in/index.html not found.
You have to actually have a file named index.html. Right now you don't. The rewriting/redirecting is working fine, you're just redirecting to a non-existent page/file.
I'm a little confused as to what you're actually trying to do. If you just want to move index.php to index.html, rename the file. Rewriting makes it so that if someone tries to open index.php they will be redirected to index.html, but you still have to have an index.html file for them to be redirected to.
RewriteEngine On
# Send the user to index.html
RewriteRule ^index.php$ /index.html [R]
# Tell the server that index.html really means index.php
RewriteRule ^index.html$ /index.php
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php
RewriteRule ^index\.php$ /index.html [L,R=301]
RewriteRule ^index\.html$ index.php [L]
The first rule redirects every direct request of /index.php externally to /index.html. And the second rule rewrites requests of /index.html internally to /index.php.
Are you sure mod rewrite is enabled & working?
1) create an html page called found.html with whatever you want in it, but some text to be sure it's loaded (not a blank page basically) and put this in an file called ".htaccess" :
RewriteEngine on
RewriteBase /
RewriteRule ^find.html$ /found.html [L]
2) upload both your .htaccess and the found.html files in your domain's root
3) Just try to load -www.example.com/find.html (with your real domain of course). If mod_rewrite is available, you should see the content of found.html while browsing find.html (which does not physically exist by the way).
If it does not work, try :
RewriteEngine on
RewriteBase /
RewriteRule ^find.html$ found.html [L]
In the Apache Conf file, you also need to make sure that AllowOverride is set to a value that will allow .htaccess to be processed.
Normally it's AllowOverride all
RewriteEngine On
RewriteRule ^index.html$ index.php
RewriteRule ^$index.htm/$ index.php [L]
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index.html$ index.php
Try this code...
It will work for your problem /..
Best Of LUck
If it solve your problem..
Visit my site

Categories