Site broken after modifying .htaccess file - apache

I recently added these lines to my .htaccess file on the server:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
What I was trying to do was eliminate the need for .php after the file name. This worked wonderfully, but something else was broken.
The URL for the website was http://subdomain.something.domain/eCom/ and I had an index.php file in it, which was automatically loaded when the above link was used. But after editing the .htaccess file, the above link returns a 404 error. The page still opens correctly if I use http://subdomain.something.domain/eCom/index or http://subdomain.something.domain/eCom/index.php. I don't know what's happening. Anyway this can be resolved?
Note: I don't understand the code above about all the RewriteEngine stuff. I just copied from a website hoping for a quick fix. Any additional information about it is welcomed, if that's what's breaking my code.

Add RewriteCond %{REQUEST_FILENAME} !-d above your first RewriteCond to avoid matching directories as well.

Related

I'm getting jquery issue after writing .htacess

I'm writing .htaccess for my website.
But after writing .htaccess for some pages, I found that redirection is working but css and jquery is not loading properly. I'm trying in localhost (xampp). Can somebody help me getting out from it.
This is my .htaccess file code.
RewriteBase /raman/
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*) index.php?$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^newdashboard$ dashboard.php
RewriteRule ^createbook/(.*)$ create_book.php?creatbookfailed=$1
Now, for example, I want to go the create_book.php page, then, it will redirect me to the createbook page but the css and jquery is not loading and giving errors. you can see the errors in the screenshot attached.
I removed the .htaccess and then, run with the original path (create_book.php?creatbookfailed=pathmessage). Jquery is working fine.
Can somebody help me to find out what am I doing wrong here.
Or need some more explanation which I missed now, please let me know
I forget to add the base url in my header file . After adding base url in header file (inside head tag), everything is working fine.
<base href="https://www.yourbaseurl.com">
Thank you for your answer #Teemu

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.

Forwarding all htm file extensions to a a specific file

I have been given a task to change 100's of .htm files on a site to only show the contact page. As appose to changing the files I am thinking to just use a .htaccess file and force all requests which includes a ".htm" or ".html" to the contact.htm page.
I have very limited knowledge about .ht_access files and the one I have which forces all requests through a index.php isn't working because one of the rewrite conditions is to check whether the file is real or not.
Any advice or tips will greatly be appreciated:
// My .htaccess rules at the moment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ contact.htm [NC,L]
You can try something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^\.]*\.htm$ contact.htm [NC,L]
This will replace everything that ends with .htm to contact.htm
EDIT: Made a mistake, checked it and fixed it
If you want to maintain your SEO and prevent link rot (which is bad), you'll want to let the client know that the old HTML file is no longer valid:
RedirectMatch 301 ^.*html?$ http://example.com/contact.html
Edit: as an interesting side note, returning a 410 might be a more appropriate response.

.htaccess display /base1/folder/ using the content of /base2/folder.html

When I open the url http://mysite.com/base1/folder/, I want it to display the contents of http://mysite.com/base2/folder.html. There is an index.html file in the folder folder.
To be clearer, the html file folder.html resides in the folder base2.
My .htaccess file does not work as expected
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/base1/([^/]+)/(.+)$ /base2/$1.html [L]
Could anyone please tell me is wrong with my .htaccess file?
Thanks!
Your regular expression looks wrong:
^/base1/([^/]+)/(.+)$
^^^ requires one or more characters
This should work (to accept anything following /folder/)
^/base1/([^/]+)/
Or this (to accept only /folder/)
^/base1/([^/]+)/$
Furthermore I would remove the two RewriteCond as your rule. (Though they shouldn't do any harm if /base1/ doesn't exist)

Ignore Rewrite Rule for images folder

I have a .htaccess file on a website I'm working on which rewrites urls from mydomain.com/sub/folder/ to mydomain.com?index.php?controller=sub&view=folder
Unfortunately the way I've written it means I can't access images, stylesheets and other linked files anymore. Could anyone tell me how best to exclude specific directories / URL requests from the rewrite rule?
Apologies if this is a bit of a newbie question, I'm still wrapping my head around this mod rewrite stuff!
The .htaccess file looks like this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?Controller=$1
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-_]+)/? index.php?Controller=$1&View=$2
If your images are in mydomain.com/images and you are linking to them using relative links on the page mydomain.com/sub/folder/ the browser is going to try to attempt to access the image via mydomain.com/sub/folder/images/i.gif. But if you change your links to absolute links, the browser will correctly attempt to load mydomain.com/images/i.gif. However, the RewriteRule will change it to: mydomain.com/index/php?Controller=images&View=i.gif. To avoid this you need to add a few RewriteConds:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]+)\/?$ index.php?Controller=$1
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)\/? index.php?Controller=$1&View=$2
So that when attempting at access an existing file/directory, don't rewrite to index.php.