When force trailing slash my href is changed - .htaccess

My .htaccess code is the following:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
It works good for me since if i use the www.domain.com/login link, I get back the following www.domain.com/login/ which is good.
The only problem is, that my href link becomes: www.domain.com/login/login which is very bad. Of course when I click on it I get the 404 error which is normal.
Can anyone help to avoid this problem!

Sounds like a relative vs absolute path issue. When you get redirected to /login/, the base URI becomes /login/ instead of just /. So either make your href an absolute URI or specify a base:
link
or add this to the header of your page:
<base href="/">

Related

.htaccess > redirect all subfolders to root (ignore trailing slash)

My gosh - I hate .htaccess ... and don't understand how the hell anything is working.
I am trying to ignore 'subfolders' and use these as guest identifyer.
Ex:
User types > http://subdomain.domain.com/guest_id
Server sends > http://subdomain.domain.com/index.php
Browser shows > http://subdomain.domain.com/guest_id
My htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]
The code works great... unless there is a trailing slash.
In this case, all files are served as 'index.php'.
The main Index.php is showing properly, but all external scripts / css / etc. are served as /guest_id/index.php (thus... do not load).
FYI, there might be a conflict as my 'subdomain' is actually invisibly serving domain.com/event/
I have been trying so many permutations... without really understanding how htaccess work.
Your help will be sooooo much appreciated.
1,000 thanks in advance!
Damien
When you have URL such as http://subdomain.domain.com/guest_id/ and are using relative links for your css/js/images then browser attempts to resolve all links by prefixing them with current path hence src='image.png' is resolved as http://subdomain.domain.com/guest_id/image.png instead of http://subdomain.domain.com/image.png.
You can have a rule to force remove trailing slash at the end of all non-directory URLs.
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Also you can add this just below <head> tag of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

Styles and files not working after .htaccess rewrite rule

I've just added a simple rewrite rule to my .htaccess file to drop .php from this page http://themeat.in/register.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
but now when I go visit that page without the .php (http://themeat.in/register/) all my styles and files have vanished. When I open up the console I see the page name is being treated as a folder.
This is what the file path should be and was before the rewrite, http://themeat.in/css/styles.css
and this is what it is now,
http://themeat.in/register/css/styles.css
I guess it's got something to do with the trailing slash within the rewrite but I'm totally stumped at how to fix this problem? I need the .php dropped and I'd like to keep the trailing slash.
Any help would be greatly appreciated.
Many thanks,
//C
This is because of the rewritten urls. when the url is example.com/register/ apache thinks /register/ is a directory and appends that in front of all relative urls.
To solve this, You can add the following base tag in head section of your webpage :
<base href="/">
For more info, see this post : Seo Friendly Url css img js not working

.htaccess redirect all subdirectories to root (Kinda)

This is not as straight forward as the title might imply. I'll try to explain.
I'm currently working on a video website based on rewritten urls.
I'm using this rule currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?url=$1 [L,QSA]
This is used to let users access videos with good looking urls like this:
domain.com/kJbSGe5X instead of domain.com/?v=kJbSGe5X or domain.com/index.php?v=kJbSGe5X for example.
Now the problem is that whenever a trailing slash is added, the css breaks.
I've tried solutions like adding a slash in front of the css url, like this:
<link href="/css/bootstrap.css" rel="stylesheet">
... but it's not working.
Could a solution be to rewrite all urlstrings after a trailing slash (including the trailing slash) to the same url, without the trailing string? Like this:
domain.com/kJbSGe5X/ or domain.com/kJbSGe5X/randomchars to this:
domain.com/kJbSGe5X - and how would I go about doing so?
I guess there probably is much better solutions to this problem, but I'm rather new at this.
Any help is appreciated.
Thanks in advance!
--EDIT--
I would prefer a solution where everything after a trailing slash gets redirected to the same url without the trailing slash + any string after the trailing. (If there is no content in said url)
I might have put to much emphasis on the css issue - A rule like this would work great with how my website is setup.
Insert this rule before your existing rule to remove any trailing slash:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^/]+)/.*?\sHTTP/ [NC]
RewriteRule ^ /%1? [R=301,L]
You could try ignoring the css directory in your rewrite rule:
RewriteCond %{REQUEST_URI} !^/css/
This isn't exactly what you asked for but if you excluded specific directories from your rewrite rule (probably /css, /js and so on) then you would not have to worry about formatting your nice/short view URLs to remove anything after the slash or whatever else.
Here is my complete solution. With a little help from anubhava!
Remove trailing slash from all urls:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1? [R=301,L]
Constrain the "create goodlooking" urls thingy to only work when it's 8 characters (which is the length of each shortlink for videos):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.{8})$ index.php?url=$1 [QSA,L]

mod-rewrite forwarding without changing URL

I have a small problem with my Apache configuration when creating "pretty" URLs. I've got it to the stage where typing (or linkig for that matter) to
index.html
forwards you to
index.php?pageID=Forside
that is exactly what I want.
But how can I get index.html to stay in the address bar of the browser?
Right now it forwards and changes the URL to the original one.
Here my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} index\.html
RewriteRule .* http://www.radoor-designs.dk/index.php?pageID=Forside [L]
And before someone comments on it: Options +FollowSymLinks is missing since it triggers an error 500 on a one.com webhotel.
Thanks in advance!
Try the following:
RewriteEngine On
RewriteRule ^index\.html$ /index.php?pageID=Forside [L]
I think this may help you to resolve your problem.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html$ /index.php?pageID=Forside [L]
This will do the redirect for you whilst showing index.html in the browser window.
Strange that symbolic links creates an error 500,
if you want it to redirect to index.html?pageID=Forside then do
RewriteRule .* /index.html?pageID=Forside [QSA,L,R=301]
I'm not 100% certain what you are trying to achieve with this could you explain a little more?

Info with mod_rewrite .htaccess

I have been trying forever to get rid of the .php from my site. I want it to look like example.com/about instead of example.com/about.php. I have it to were it will rewrite it so i can type it in as example.com/about but i want it to were when i click on a link on my website to go to example.com/about it loads the url as example.com/about and not example.com/about.php. Overall i would like it so that whenever someone loads anything on my page the end of the url doesnt have the .php. My .htaccess right now looks like so,
RewriteEngine on
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
Thank you.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You wouldn't have to change your site's structure as it simply targets "malformed" URIs in the form of "http://x.com/page" without an extension at the end. Hope I helped!
but i want it to were when i click on a link on my website to go to example.com/about it loads the url as example.com/about and not example.com/about.php
To fix that, you need to change links from your site from example.com/about.php to example.com/about. You can still do a redirect when links "in the wild" still point to the .php URLs, but unless you change the links on your site to remove the extension, your're causing every page load to hit your server twice.
As for the redirect:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.php[\?\ ]
RewriteRule ^ /%1/ [L,R=301]
I'm not sure how your rules to put the php extension back is even working, since you're matching for a trailing slash but when you check the file with -f, you'll have a stray trailing slash in your filename: e.g. /some/path/to/the_file/.php instead of /some/path/to/the_file.php
Try:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+?)/?
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]

Resources