Getting the trailing slash back in ExpressionEngine 2? - expressionengine

I have just upgraded from EE 1 to EE 2 and I am struggling with some of the changes. For example, the fact that EE no longer outputs a trailing slash on its urls is making a mess of a lot of my links where I had depended on constructions like EE generated url + additional segment. Finding and editing all places where I have done that would be a small nightmare considering the size and setup of my site. Is there any way to hack EE to get back the old behaviour?

For some the trailing slash addition mentioned might cause conflicts with some forms. Adding the following to check if the request is a GET might be safer.
# Add a trailing slash to paths without an extension
RewriteCond %{THE_REQUEST} ^GET
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

There is an .htaccess solution to this, which I've used in my older EE sites because of this issue exactly.
Add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
Source: http://devot-ee.com/articles/item/simple-htaccess-for-expressionengine-sites
However, since it's there by default in EE2, you might want to do the OPPOSITE and remove the trailing slash via .htaccess so you don't have to manually add/delete/whatever. If you decide on that, here's the code:
Remove Trailing Slash
RewriteCond %{HTTP_HOST} !^\.yoursite\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
Source: http://ee-spotlight.com/tips/a_standard_htaccess_file_with_expressionengine

Related

.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]

Removing .php file extension from the end of a url.

I am trying to achieve something very simple on my web server. I want urls that end in .php to end with / for example www.example.net/something.php to be www.example.net/something or www.example.net/folder/subfolder/something.php to be www.example.net/folder/subfolder/something. Additionally I don't mind if I have www.example.net/folder/something.php?something=whatever/. I am only concerned with removing the last .php in the url. I have used this code amongst many others.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteRule (.*)/$ $1.php [L]
However this only works for some of my pages. For example it does not work on my www.example.com/folder/contactus.php page. Instead I get my 404 page. But it works on www.example/folder/faq.php perfectly.
Additionally I make some file_get_contents() php request within various parts of my code. Is there any way to tailor these rules so internal requests (my php functions) are not affected. Basically I only want the browser to display the url without the .php end without affecting my other php functions that are held in .php files. For further clarification of my intentions: I use a string processing functions to extract particular lines of text from a php file based on variables I have set up. But with the current rewrite rules the php file cannot be found because the extension has been removed from the filename (im guessing.
$file_contents = file_get_contents($filename);
I'm currently using a linux server on a shared godaddy host account.
I think you need to apply the php check to both rules:
RewriteCond %{REQUEST_URI} ^(.*)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule (.*)/$ $1.php [L]
RewriteCond {REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
Additionally, the -f check needs to be made without the trailing slash
The following worked as desired, though one of my pages used get request which failed. I just changed it to a post request and all was well.
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /([^.\ ]+\.)+php(\?[^\ ]*)?\ HTTP
RewriteRule ^(.+)\.php$ http://www.sitename.net/$1 [R=301,L]
RewriteRule ^([^/]+/)*index/?$ http://www.sitename.net/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://www.sitename.net/$1 [R=301,L]
RewriteCond $1 !^([^.]+\.)+([a-z0-9]+)$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*[^/])$ /$1.php [L]
RewriteRule (.*)/$ $1.php [L]
One small problem still remains. Some pages the trailing slash is appended and others they are not. Any clues on forcing the trailing slash?

HTaccess - Trailing slash + .php + anchors (ExpressionEngine)

I just started a project from another webmaster work (i hate patching other's stuff ::sad::) and i have some issues. First we are on expression engine 1.x .
My problem: There is a trailing slash redirection in the .HTACCESS, but my users need to have access to only one .php page (www.mydomain.com/mobile/index.php) but the link is redirected to /index.php/, another problem is the anchors are changed to the same way (www.mydomain.com/somepage/#anchor1) to /#anchor1/
So my question is... there is a way to put exception into trailing slash redirection code? I mean i just have to fix it in few pages. Take note that our expression engine remove all the index.php to have links like www.mydomaine/contact/, www.mydomaine/about/, www.mydomaine/infos/ , ect.
Currently the htaccess trailling code is :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
P-S: we have a code that remove index.php too:
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/(members|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
Thx for help!
Is there a valid reason trailing slashes in URLs need to be enforced? If not, I would just remove the rewrite rule and your problem is solved :)
Most ExpressionEngine URLs will work with or without trailing slashes.
The easiest fix to this problem would be remove the overzealous trailing slash redirection, and replace it with a more relaxed version:
<IfModule mod_rewrite.c>
# Enable Apache's RewriteEngine
RewriteEngine On
# Add Trailing Slashes to URLs
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ /$1/ [R=301,L]
# ExpressionEngine Remove index.php from URLs
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Using the above code, example.com/mobile/index.php will not get rewritten to example.com/mobile/index.php/, nor will pages with anchors example.com/page/#anchor1.
EE doesn't know the difference between URIs like /mobile versus /mobile/, web analytics apps and search engines may consider these separate web pages. If you're developing a static website, this isn't a big deal, because if you attempt to go to the former URI (sans trailing slash), Apache will automatically redirect the client to the latter (with trailing slash).
But for a web application like EE, where everything in the URI after index.php is handled by the application rather than Apache, this redirection is left up to you. Just like the decision to use or not use a www subdomain, it doesn't matter whether you choose to force a trailing slash or vise-versa; it just matters that you enforce one or the other.
Sidenote: in EE1, trailing slashes are generated in URLs produced by
ExpressionEngine; in EE2, trailing slashes are not generated. The exception
is the Structure Module, which outputs URLs with trailing slashes
in both EE1 and EE2.

Trying to add trailing slash with htaccess, results in a absolute path

What I'm trying to achive is to have all urls on my page look like http://domain.com/page/, no extensions, but a trailing slash. If a user happends to write http://domain.com/page or http://domain.com/page.php it will redirect to the first url. After some googling i found this code, and it's close to working, but when you leave out the trailing slash in your request the url becomes something like http://domain.com/Users/"..."/page/ and therefor returns a 404.
My .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !(.*)/$
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
I've been trying to add an additional rule but I really don't get any of this and I haven't been able to find any answers.
For a scenario like this one, the .htaccess author has to consider both what the browser URL bar should display and what file the web server should return/execute. Note also that each external redirect starts the processing of the rewrite directives over.
With that in mind, start by taking care of which file is returned when the URL is in the correct format:
RewriteEngine on
RewriteRule ^/?$ /index.php [L]
RewriteRule ([^./]+)/$ /$1.php [L]
Then, deal with URLs with no trailing slash by redirecting them with [R=301]:
RewriteRule ^/(.*)\.[^.]*$ http://www.example.com/$1/ [R=301,L]
RewriteRule ^/(.*)$ http://www.example.com/$1/ [R=301,L]
Note that the first of these two rules should also take care of the case where there is a filename (like something.php) but also a trailing slash by eliminating the filename extension and re-adding the slash.
Keep in mind that, if your internal directory structure does not match what the web server is serving (as is often the case in shared hosting scenarios), you will likely need to add a RewriteBase directive immediately after the RewriteEngine directive. See the Apache docs for an explanation.

htaccess - forcing trailing slash two directories deep

My website structure has a root /index.php, some files as /directory/index.php and some as /directory/(filename).php
I have the following .htaccess which removes the php extensions and the "index.php" for my URLs, and forces trailing slashes on the first level directories for SEO goodness:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
so the following are working (they show the correct page):
/
/directory/
/directory/filename/
The only thing that doesn't work, is if I type in:
/directory/filename
It goes to:
http://(mylocalurl)/Users/(myusername)/Sites/(mysitedirectory)/directory/filename/
My question is: How do I make the second level filename rewrite to force a trailing slash like:
/directory/filename/
Thanks for your help!
DirectorySlash on
will add a trailing slash where appropriate.
Not 100% sure, but it could be that it is triggered by your the second RewriteCond, the RewriteRule is run, not replacing anything, and then you have the [L] that makes it not go to the last RewriteCond.
Maybe changing the order of the two might help?
(Or else, you might want to look into Multiviews directive)
When you're specifying a local relative URL path to perform an external redirection on, that path needs to be prepended with a slash. Since the match to your RewriteRule test pattern will not have a leading slash, be sure to put one in the rewrite:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ /$0/ [L,R=301]
If I've understood your problem correctly, that's the only issue you have. If you needed something else though, let me know.

Resources