I'm getting jquery issue after writing .htacess - apache

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

Related

Site broken after modifying .htaccess file

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.

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.

Display content based on URL

In my site I had a desire to make simpler URLs such as:
http://www.example.com/about
Instead of:
http://www.example.com/about.php
Mostly for aesthetics, for now I have Apache redirect to ./default.php if a directory is requested. However this forces me to create a directory at
http://www.example.com/about/ and a file inside it called default.php ending up with:
http://www.example.com/about/default.php
I know there's a better way, probably using PHP or JS, what do?
it's all in apache's configs,
rewriteEngine can do that for you
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
A snippet taken from third link:
To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code to your Apache config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want to remove the .html extension:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can even link pages inside the HTML document without needing to add the extension of the page. For example:
wallpaper

Mod-Rewrite rules are breaking 404 routing

I am using the following mod-rewrite in my .htaccess file:
RewriteRule ^$ pages/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ pages/$1 [L]
The intention is to hide the subdirectory called /pages/ from displaying in the URL.
So this: http://mysite.com/pages/home.html
Will look like this: http://mysite.com/home.html
It works but there are some unintended consequences.
As a direct result of the .htaccess code I posted above, my 404 routing is no longer working at all. Anything that should trigger a 404 error page is instead generating a 500 Server Error.
How to fix?
EDIT:
As implied above, it does not matter if a custom 404 page is defined in the .htaccess or not. Without it, or a bad path to the error page, the server should still route to its default 404 page, and not give a 500 Server Error.
Surely, there must be a standard way to suppress sections of a URL without breaking the normal routing of 404 errors. From my online research it seems that my method above commonly breaks the 404 routing, and yet so far, I've seen no applicable solution. (This is not a Wordpress installation; just static HTML content)
EDIT 2:
Since I'm only wanting to suppress the one directory from the URL, I never mentioned that I also have other files & directories which are siblings to /pages/ that cannot be pointed at /pages/, such as /graphics/, /includes/, /css/, /cgi-bin/, robots.txt, favicon.ico, etc.
Maybe this is all an exercise in futility or more trouble than it's worth?
Looking for a definitive answer either way.
Following config will look for your static pages inside the pages/ and if found, it'll display them. This shouldn't break 404.
Put it in root folder of your web in .htaccess
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/pages/%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/pages/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /pages/$1
This should achieve what you are trying to do.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9\_\-]+)\.html$
RewriteRule (.*) /pages/$1 [L]
Thank-you to #Kamil Šrot for getting the closest working solution. However, I needed to add another test ( -d ) to see if the requesting URI is a directory.
This is working great and the 404 error page is again routing properly.
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/pages/%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/pages/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /pages/$1
How about adding an error page direction to your htaccess file to handle the 404 page:
ErrorDocument 404 /path/to/your/404.html

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.