mod_rewrite rule error when using / - apache

I have the following RewriteRule in my .htaccess file:
RewriteRule ^advanced-lift-truck?$ pub-listing-full.php?mag=1 [NC,L]
How could I modify this so that .../advanced-lift-truck/ works, as well as .../advanced-lift-truck? (Note the / in the first version.)
I'm assuming I'll need a separate rule, but I've tried the following also:
RewriteRule ^advanced-lift-truck/?$ pub-listing-full.php?mag=1 [NC,L]
but this forwards me to pub-listing-full.php without the query string, and without any CSS/JS files loaded.

Not sure why the query string is missing, but you need to deal with all of your relative URLs in the links of your page. With the extra /, browsers assume the base URI to resolve any relative links (to stuff like style sheets and scripts) is /advanced-lift-truck/. Try adding this to the header of your pages:
<base href="/" />

Related

redirecting about.php to domain.com/About/ using .htaccess rewrite rules

When I write RewriteRule About About.php [L,QSA] in .htaccess to make users access example.com/About.php using the following link example.com/About/. Users can access it correctly if it was written as example.com/About only. If I added more backslack like this example.com/About/ the CSS files are not loaded. Also, if anyone accessed this link example.com/About/SomeText he didn't get error 404 he gets about.php page without loading CSS as well.
RewriteRule About About.php [L,QSA]
The problem with this rule is that it rewrites any URL that simply contains the word "About". So, /About/SomeText, /AnythingAbout and /AbcAboutXyz are all rewritten to About.php which processes the request. (It also rewrites itself, which is another matter!)
You need to be specific and match only requests for /About exactly. For example:
RewriteRule ^About$ About.php [L]
The first argument to the RewriteRule directive is a regex that matches against the requested URL-path (less the slash prefix).
The QSA flag is not required here.
If I added more backslack like this domain.com/About/ the CSS files are not loaded.
"backslack" - that is a (forward) "slash" (and not a "backslash" either).
This will happen if you are using relative URL-paths to access your CSS files (and other static resources like images and JS). The relative URL-path is resolved by the browser relative to the current URL. eg. If you are linking to your stylesheet with href="styles.css" and the current URL in the browser is /About/SomeText then the browser will naturally request /About/styles.css, not /styles.css as you might be expecting (when the URL is simply /About).
When rewriting the URL at different path depths you need to use either:
Root-relative (starting with a slash) URL-paths.
Absolute URLs (with scheme + hostname).
Specify a base element in the head section of the HTML source that tells the browser what URL should be used as the base for all relative URLs. Although there are caveats with this approach (as mentioned in the linked question below)
(Although, in restricting the above rule to match /About only then this naturally avoids this issue.)
See also my answer to the following question that goes into more detail on this:
JS and CSS Rendering Issues After .htaccess File URL Rewrite Rule

Problem with htaccess when making friendly urls with the trailing slash does not work [duplicate]

I want to make my URL as SEO Friendly URL. I tried editing .htaccess file by rewriting rule
RewriteRule ^swift-details/([0-9]+)/([0-9a-zA-Z_-]+)$ swift-details.php?id=$1 [NC,L]
RewriteRule ^swift-details/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
It's routing the correct URL but in that page CSS JS and images are not working.
Example URL:
http://www.example.com/swift-details/2/abblinbb
This is because your relative URIs have their base changed. Originally, the base is / when the page is /swift-details.php?id=foo, and the browser properly fills in relative links with the / base. But when the browser goes to a page like /swift/details/foo the base suddenly becomes /swift/ and it tries to append that in front of all relative URLs and thus none of them load.
You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head> tags):
<base href="/">
You dont need the second rewrite rule. Your CSS/JS paths are all 'relative' to your current location.
Your CSS exists here:
/css/normalize.css
Your page is looking here:
/swift-details/2/abblinbb/css/normalize.css
All you need is 'forward-slashes' before your CSS/JS paths.

Why can't i have slashes in my rewrite rule?

I am trying to make an .htaccess file, to create SEO url.
It seems however that I can't have a slash in the mod rewrite, am I doing this wrong?
My code is as follows:
RewriteEngine on
RewriteRule ^(.+)/$ /$1 [L,R=301]
RewriteRule ^folder/folder-name index.php?show=folder&folder_id=7
The page is loaded correctly, but the style sheet/images/javascript aren't found!
It's as if, the page thinks that it should look for the style/images/js relative to "folder/folder-name".
If i change the last line to:
RewriteRule ^folder-folder-name index.php?show=folder&folder_id=7
Then everything is loaded correctly, so i'm pretty sure it's because of the slash.
Any help?
It is because you're using relative paths in your JS, CSS etc. You have 2 options to fix it:
Just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can try adding this tag in your page's header:
<base href="/" />
One alternative: You can use paths relative to site root.
i.e. change:
<img src="images/some.jpg" />
by
<img src="/images/some.jpg" />
Being images folder here: yoursite.com/images

how to rewrite url with htaccess and keep relative links working

I am using the following .htaccess rules:
RewriteRule ^!([a-zA-Z0-9_-]+)$ index.php?p=$1
RewriteRule ^!([a-zA-Z0-9_-]+)/$ index.php?p=$1
RewriteRule ^!([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?p=$1&s=$2
RewriteRule ^!([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?p=$1&s=$2
In order to rewrite the url from this: /index.php?p=SOMETHING&s=SOMETHING
To this: /SOMETHING/SOMETHING
The problem is - I am using relative urls for pretty much everything - css styles, scripts, images, etc.
And the current htaccess rules break the urls, cause they are trying to access files from a realtive path.
So I would like to know if there is any way to be able to still use these rules and keep the relative links working at the same time.
Any help is much appreciated!
You need to include the URI base in the header of your pages. You can add something like this in your page headers (inbetween the <head> </head> tags:
<base href="/">
Either that, or you can change all your links to absolute.

mod_rewrite changes MIME types .css/.js -> html

I have an issue with mod_rewrite, I have a .htaccess file set up in directory called cms:
Options +FollowSymLinks
RewriteEngine on
#rewrite rules for edit
RewriteRule ^edit\/(.*) edit.php?page=$1 [QSA,L]
I would like to access it like so sitename.com/cms/edit/2 but when I do I get errors:
When I access the natural path (sitename/css/edit.php?page=2) everything works fine.
Any help would be appreciated.
While this is an old issue, i just experienced the same thing but then when i used RewriteRules.
The solution for that turns out to be:
RewriteRule ^bower_components/(.*)\.(css) test/bower_components/$1.css [L,T=text/css]
RewriteRule ^bower_components/(.*)\.(js) test/bower_components/$1.js [L,T=application/javascript]
To force the type of the files you are rewriting. Targeting css + js specifically and forcing the T=<>
Not sure if you had a typo, what about changing:
RewriteRule ^edit\/(.*) edit.php?page=$1 [QSA,L]
To:
RewriteRule ^edit/(.*?)/?$ edit.php?page=$1 [QSA,L]
I know its old question, but just in case someone will still search for an answer.
I just witness same errors.
The deal is: with L parameter mod_rewrite will point request to the location you specified but all the ajax calls, includes, javascript src requests will behave as you where under original location.
Check the headers using LiveHeaders in Firefox for instance and you will see that your requests are pointing to wrong locations. I.e.: if in your play.php you link javascript like
src="myJS.js"
if RewriteRule points from
[siteroot]/play/some_data_here
to
[siteroot]/play.php?data=some_data_here
you will see that your play.php file will try to include src="play/myJS.js" which of course is not there so it returns 404 aka text/html.
In my case I 'fixed it' by adding R parameter so it redirects to new location. Down side of this is that address in the browser will change. Try changing [QSA,L] to [QSA,L,R].
Hope this helps.
I was having this problem as well and found a solution.
If your css / js files are linked with a relative path, your RewriteRule will also affect them. To avoid this, reference the root directory. In other words:
<link type="text/css" rel="stylesheet" href="css/style.css">
becomes
<link type="text/css" rel="stylesheet" href="/css/style.css">