.htaccess Redirecting url to a folder. Error on loading resources - .htaccess

I need to redirect from URL to a folder.
So, in the htaccess file:
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^(.*) http://www.example.org/portal/$1 [P]
But, when the portal app is requested it calls many .css .js .png files...
When these files are requesteds, the URL works like this:
www.example.com/portal/file1.css
www.example.com/portal/file2.js...
and I receive the 404(file not found)
It because when the app requests the files, the htaccess intercepts the call and reirect it to www.example.com/portal/portal/file1.css
So, i need when the exactly domain www.example.com is called, the rule redirect to www.example.com/portal. But when some resource of portal be called, the redirect must be do to www.example.com/resource. Internally it working like: www.example.com/portal/portal and i receive the 404 error
Help-me please.
Thanks

Probably you're using a relative path to your assets href="/portal/file.css" and the root url is www.example.com/portal
Try using the links without directory like href="/file.css"

Related

Trying to rewrite a local url to another

I want to be able to do the following:
ReWrite /test -> /test.php
Also:
ReWrite /Test/test -> /Test/test.php
I want to be able to do this only for those mentioned urls. Also, I do not want direct access to .php urls. For example, if the user navigates to /Test/bla.php or any other links, he would be automatically be directed to the /index. The main reason I am doing this is that I do not want the user to know I am using .php. Here is what I tried:
RewriteEngine On
RewriteRule ^Test/test$ /Test/test.php
RewriteRule ^test$ test.php
ErrorDocument 404 /index.php
This works to some extent. I can navigate to /Test/test and it redirects me to /Test/test.php without issues. Though, if I do something like /test (from the root), I see the Test folder instead. Also, 404 does not work in this .htaccess above. I can navigate to any missing url and it complains that it is not there.
I am new to web hosting. So, I am kind of confused by the .htaccess.
Thanks for your time.

.htaccess rewrite with subdirectory

I want to rewrite (not redirect) all url's of my site to a sub-directory of my site. For example:
http://example.com/example
would load the following:
http://example.com/public/example
Though, requesting http://example.com/public should not load the contents from public/public but from /.
Answers I've found on SO either do the above with redirect (which I don't want) or doesn't account for the special case above.
EDIT: further clarification:
I want every request on my site to go load under the public folder, but without being visible to the visitor. So requesting http://example.com/index.php will load the file from http://example.com/public/index.php. The url in the browser remains unchanged for the user.
Try the following rule :
RewriteEngine on
RewriteRule ^((?!public).+)$ /public/$1 [NC,L]
This will rewrite all requests from root to /public dir.

mod_rewrite not working the same in HTTPS as HTTP

UPDATED:
I currently have a .htaccess file in a folder called /pages/ that contains the following:
RewriteRule ^(.*)$ ../page_rewrite.php [L,QSA]
This then passes the path to page_rewrite.php when someone accesses www.something.com/pages/some/page-to-go/to/
This works perfectly when i access the site via
http://www.something.com/pages/some/page-to-go/to/
but i get a 403 when i go via https
https://www.something.com/pages/some/page-to-go/to/
accessing all php pages directly works just not the rewrittern URL.
Any ideas? i have been googling it but cant find what im looking for.

When trying to redirect using HTACESS, a string "/redirect:/" gets embedded with the url

I have setup two domains to point to the same hosting server. For eg: the domains be :
example1.com
example2.com
I have created a directory called 'home' in my root, and whenever the 2nd domain is accessed, it should get redirected to the home directory.
For eg: example2.com/post.php should get redirected to example2.com/home/post.php
I have done this by creating an htaccess file in my root directory with the following contents:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteCond %{REQUEST_URI} !home
RewriteRule ^(.*)$ ./home/$1
This is working perfectly. Now, i have a second requirement where i need to redirect users to a php file (post.php) inside home directory whenever the user accesses the url example2.com/gallery/somestring.
So, i created a second htaccess file in my home directory and entered the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^gallery/([^/]+)$ post.php?post=$1
But, whenever i try to access the url example2.com/gallery/somestring, it shows a strange 404 Not Found error :
The requested URL /home/redirect:/home/gallery.html/somestring/somestring was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Here. "redirect:" string is getting concatenated to the redirected URL somehow.
Can any one please give me some idea as to why its happening ??
I solved this problem. I am sharing this if any one gets into this same problem and finds this post. The problem was that there was a file gallery.html in my root folder. This was causing this error. Just delete this file or rename the file to some thing else. Thats it :)

simple .htaccess rewrite URL to different directory on same server

Ok, I'm clueless here...
I need to rewrite a directory structure and all sub-directories within it to a directory within the same server, but a root that is before the directory.
For example:
http://www.mydomain.com/Themes/default/css/folder
and all directories called upon after folder. Such as folder/sub_folder or folder/afolder/anotherfolder, it needs to include ALL sub-directories within the folder directory.
should be redirected to this:
http://www.mydomain.com
How do I do this via a .htaccess file within the folder path http://www.mydomain.com/Themes/default/css/folder?
Please someone help.
Thanks guys :)
The files within the directory structure still need to be accessible for that structure when called via PHP, but I don't want people being able to browse to http://www.mydomain.com/Themes/default/css/folder and be shown all subdirectories within that folderpath and/or all files. Same thing for all sub-directories that follow that folder path.
I'd like to be able to place the .htaccess file within the http://www.mydomain.com/Themes/default/css/folder directory on the server, but don't know exactly what code to use for this.
ALSO, even more challenging... The domain name can change, so I'd rather not use the domain name within the .htaccess file, instead perhaps use .. or . to go up a directory or a different method of grabbing the domain name within the .htaccess file.
Create a .htaccess file in /Themes/default/css/folder and place these lines there (it requires mod_rewrite):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ http://%{HTTP_HOST}/ [R=301,L]
It will redirect (301 Permanent Redirect) all requests to a folder to a homepage. If file is requested, it will allow it.
If you want to have it working for folders as well as files then remove the RewriteCond line -- it will redirect ALL requests (even for non-existing URLs) to a homepage.
If you will see "500 Internal Server Error" after creating such file, then it is your server configuration: mod_rewrite may not be enabled or it's directives (RewriteRule, RewriteCond, RewriteEngine) are not allowed to be placed in .htaccess. In any case -- check Apache's error log for exact error message (it will give you the exact reason).
http://www.besthostratings.com/articles/prevent-directory-listing.html
IndexIgnore *

Resources