Page not found when rewriting URL through .htaccess - apache

I've been having problems trying to set up my .htaccess for the site.
I am trying to remove a folder from the URL when a page accesses this. I managed to successfully remove the folder from the path using this post How can I remove this directory/folder from url with htaccess rewrite? but when I load the page up, none of the css, javascript or other resources are loaded correctly.
When I try to access the resource from the url it says 'The requested URL /modules/content/styles/default.css was not found on this server.'
Modules is the folder I am trying to remove when accessing a page, however it seems that when I added the code to htaccess, it's adding "modules" to all paths.
My file structure looks like this
I have a .htaccess file in the root directory with the code:
RewriteEngine on
RewriteRule !^/?modules modules%{REQUEST_URI} [L,NC]
I also have one in the modules folder with the code:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+modules([^\s]*) [NC]
RewriteRule ^ %1 [R=301,L]
I have tried multiple solutions but all end with "Page not found", this is the closest I have gotten but now I am having a problem with resources.
Any help would be great!

You need to change this rule in root .htaccess:
RewriteEngine on
# 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
RewriteRule !^modules/ modules%{REQUEST_URI} [L,NC]

Related

.html removal from URL using .htaccess rewrite rules

I have a static website - a bunch of static html pages. I am trying to remove .html part from the URL of my webpages. I have used an .htaccess file with the following code to do that:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
However, I am getting a 404 error. For example:
The requested URL /home/username/public_html/contact.html was not found on this server.
Ideally, it should redirect to /~username/contact.html.
Addition information
When I used "Options -MultiViews" line above the code it is giving the following error:
500 Internal Server Error: The server encountered an internal error or misconfiguration and was unable to complete your request.
Apache/2.2.15 (Red Hat) server is used here.
Why I am facing this problem? Is root is automatically getting changed from public_html/ folder?
EDIT:
Directory Structure:
Username
.gnome2 (there are empty folders inside it)
.mozilla (there are empty folders inside it)
public_html (I have put css, fonts, js, etc folders and .htaccess, contact.html, index.html, etc files here.)
.bash_logout
.bash_profile
.bashrc
Username folder is under universitynameuniverse folder (whose other folders I cannot see).
Try this code in your .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can now link pages inside the HTML document without needing to add the extension of the page.
Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [QSA,L]
</IfModule>
## Results
# ~user/contact => ~user/contact.html (only if file exists)
It only works if the ~user/ directory actually exists on the filesystem.
If you need an external redirect instead, append an R flag to the RewriteRule directive.
The problem with your rewrite rule is that it's appending a .html suffix to the whole %{REQUEST_URI} variable which will probably result in a 404.
Update
Re-reading your question I noted that you want to map the ~user part to somewhere out of the apache webroot. In this case, try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/~(.+)/(.+)$
RewriteCond /home/%1/public_html/%2.html -f
RewriteRule . /home/%1/public_html/%2.html [QSA,L]
</IfModule>
## Results
# ~user/contact => /home/user/public_html/contact.html (only if file exists)

Change root directory in .htaccess

I have a problem with changing the root directory in .htaccess.
My folder structure looks like this.
What I want to achieve is, when I visit this page:
/comparty/about/
The page I will see is this page:
/comparty/pages/about/
I have already tried to search on Google, but the code I found did not work, though I tried to change it:
RewriteEngine On
RewriteBase /comparty/
RewriteRule ^(.*)$ pages/$1 [L]
I don't want it to redirect, I want to keep the same URL. Also I've had a big problem with Apache caching the .htaccess file, so I haven't been able to test many things.
Thanks in advance.
EDIT
I found a way to rewrite the URL from /comparty/pages/about/ to /comparty/about/ - this is the code:
RewriteEngine On
RewriteBase /comparty/
RewriteRule ^about/(.*)$ pages/$1 [L]
This only works on the about page, though. What would I have to do, to make it dynamic and work with every page?
You need to use a dynmic pattern :
RewriteEngine On
RewriteBase /comparty/
#if the request is not for an existent dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is not for an existent file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite the request to "/pages/request"
RewriteRule ^(.*)$ pages/$1 [L]
RewriteConditions above are important to avoid rewriting your existent files and directories to the /pages subfolder. Without those conditionrt the Rule will rewrite all requests including the destination path /pages and this may result in rewrite loop error.

.htaccess in subfolder doesn't work

I'm hosting my site on Godaddy server.
On the server I have 5 sites, each site on individual folder.
for example: "webroot/site1"
I directed specific domain to the specific folder on the server "/site1"
The problem:
I tried to make a RewriteRule with .htaccess in the subfolder but it keeps telling me 404 not found.
This is the .htaccess:
RewriteEngine On
this works fine:
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
this redirect to 404 page:
#rewirite categories only
RewriteRule ^category/([0-9]+)/.*$ /category.php?c_id=$1 [L,QSA]
this url works perfect:
www.example.com/category.php?id=1
this url doesn't work and redirect to 404 page
www.example.com/category/1/blalba
More details:
The other 4 folders are word press sites.
In the webroot there is another wordpress site.
In the webroot there is an empty htaccess file.
I already tried to do
I already tried to do:
RewriteRule ^category/([0-9]+)/.*$ /site1/category.php?c_id=$1 [L,QSA]
EDIT:
I don't have category folder is just for the URL I want that it will redirect me to category.php with the id parameter
This is the error I get:
Not Found
The requested URL /site1/category/1/area-rugs was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The RewriteRule will need to be in the htaccess file in the root folder of the website.
E.g. if the directory structure is something like this:
/webroot/site1.com/
/webroot/site1.com/.htaccess
/webroot/site1.com/index.php
/webroot/site1.com/category.php
I'd have this code in the .htaccess file in site1.com:
RewriteEngine On
# Force www
RewriteCond %{HTTP_HOST} ^site1.com
RewriteRule ^(.*)$ http://www.site1.com/$1 [R=301]
# Handle categories
RewriteRule ^category/([0-9]+) /category.php?c_id=$1 [L,QSA]

Yii framework: Using htaccess for site in sub directory not working on Hostgator

I'm trying to set up Yii site to be loaded from sub directory to root domain. In my root site folder I have only root .htaccess file and sub directory "subdir" which contains Yii site. I found a solution that works on my local environment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
But when I upload the site to HostGator it just does not work correctly
For example
if I use http://localhost/contact on my local environment, correct page is opened (called site/contact - site controller and contact action. I've added Yii 'contact'=>'site/contact' rule to the urlManager, so both http://localhost/contact and http://localhost/site/contact can work)
If I use http://mydomain.com/contact or (http://mydomain.com/site/contact) on HostGator, I get default index page (called site/index - site controller and default index action instead)
When I choose to access subsites directly like http://mydomain.com/subdir/contact it works fine, but it does not work if I use http://mydomain.com/contact
I guess that I need somehow to change this last rule in htaccess, but not sure how. Thanks!
I've found alternative solution that works. I was forced to use old fashioned URLs instead of paths, so I've changed my urlManager to:
'urlManager'=>array(
//'urlFormat'=>'path',
'showScriptName'=>true,
'caseSensitive'=>false,
'rules'=>array(
...
)
)
And my root htaccess looks now like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/index.php?r=$1 [QSA,L]
So, when accessing
http://mydomain.com/site/contact
Actual mapped URL is
http://mydomain.com/subdir/index.php?r=site/contact
which as result returns correct contact page
Since, you stated in your previous question that www.mysite.com/mysite/frontend/www/controller/action works fine; you should be using:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)$ /mysite/frontend/www/$1 [L]
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php under webroot (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory

mod_rewrite -> doesn't work as expected

i use the following .htaccess to redirect all requests to my index.php except if a folder or file exists.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri=$1 [QSA]
But if i try url's like domain.com/thing/a/thing/b i get redirected to my index.php but everything else is messed up. The html doesn't link properly to the stylesheet and so on.
My application is working kinda like a MVC-Framework, i.e. everything happens over the index.php. So i don't want that other files are accessible except files in a public folder and some specific files. Namely cron.php in the root and some css/js/image files down the folder structure.
More details on the file structure: https://bitbucket.org/BrainInBlack/source-motd-plus/src
Or do i have to link stylesheets and other stuff with absolute paths, i.e. "http://domain.com/path/to/style.css" and so on?
use a slash client before (.*)$ index.php?uri=$1.
use this code:
RewriteRule ^/client(.*)$ index.php?uri=$1
You either need to change all your links to absolute links or add a
<base href="http://domain.com/">
to the header of your pages.