htaccess mod_rewrite with trailing slashes issue - mod-rewrite

I was wondering if anyone could help? Basically I've got a set of mod_rewrite rules and they all work well except if I put a trailing slash at the end of the URL it goes crazy. Still show's the page (sometimes) except all of the CSS, JS, etc is not included because the path messes up when there's a trailing slash. The only way to solve it is to put a BASE tag which I would really like to avoid as it brings it's own set of issues. Is there something wrong with my htaccess? I just need it to ignore the trailing slashes.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^resetpassword/([^/.]+)/?$ resetPassword.php?resetCode=$1 [L]
RewriteRule ^activate/([^/.]+)/?$ activateAccount.php?ACTCode=$1 [L]
RewriteRule ^dashboard/?$ dashboard.php [NC,L]
RewriteRule ^login/?$ login.php [NC,L]
RewriteRule ^register/?$ register.php [NC,L]
RewriteRule ^messages/inbox/??$ messagesInbox.php [NC,L]
RewriteRule ^messages/archive/??$ messagesArchive.php [NC,L]
RewriteRule ^messages/??$ messagesInbox.php [NC,L]

There's likely not a good fix for this because you'll be switching "directory" levels in any case (like in messages/inbox).
The most common solution is to either use an absolute path to reference CSS, images etc.:
<link rel="stylesheet" href="/css/styles.css">
or to have a PHP variable containing the web root:
<link rel="stylesheet" href="<? echo $webroot; ?>/css/styles.css">
Both these approaches are vastly better than using a base tag, or doing some .htaccess-based redirection shenanigans.

Related

CodeIgniter and specific rewrite rule

On my CodeIgniter site, I would like to add a specific rewrite rule, so that this url
http://www.exemple.com/cache.manifest
would rewrite to
http://www.exemple.com/controller/manifest
(because Safari 7 seems to only accept .manifest files for ApplicationCache)
So I try to add this line to my htaccess
RewriteRule ^cache.manifest$ controller/manifest
I added it before the other rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^cache.manifest$ controller/manifest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
But it returns a 404. If I change the line to
RewriteRule ^cache.manifest$ test.html
it works. So the first part on my rule is correct.
If I try to access directly to www.example.com/controller/manifest, it works to, so my url is correct.
I tried also
RewriteRule ^cache.manifest$ index.php/controller/manifest [L]
But it doesn't work either…
Any clue ?
Thanks a lot
I tried some tests on my local server and I think the following might work:
RewriteRule ^cache.manifest$ /index.php/controller/manifest [R,L]
I am not entirely sure if you need the leading "/" or "/index.php", so you
may need to experiment.
You need the [R] flag to force a redirect. In this situation, you want Apache
to look for the string cache.manifest in the URL, and then go to the CI page
controller/manifest.
It appears that you need to explicitly set the redirect.
Please let me know if this works. Good luck!

Remove particular segment from url

I am using Codeigniter for one of my apps and have a following htaccess file
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
Now i have a url which is like mydomain.com/group/username
For SEO reasons i am suppose to convert this url into mydomian.com/group/username into mydomian.com/username using 301 redirect
Also urls like mydomain.com/group/username/page1 should redirect to mydomian.com/username/page
The closest i have tried after googling is by pasting this below line at the end of the file
RewriteRule ^(.*?)/?group(.*)$ /$1 [L]
But it isnt working.. any idea where i have gone wrong ?
The answers on other questions doesnt seem to be working for me/ Am pretty bad with .htaccess
P.S - i tried this
RewriteRule ^group/username(.*)$ username/$1 [QSA,R=301,L]
It works fine but it gives consecutive slashes like mydomain.com/username//pagename
I have figured it out
RewriteRule ^group/username(.*)$ username$1 [QSA,R=301,L]
This is will simply remove the 'group' from your URL and will do 301 redirect
If
RewriteRule ^group/username(.*)$ username/$1 [QSA,R=301,L]
gives two slashes, change it to:
RewriteRule ^group/username/(.*)$ username/$1 [QSA,R=301,L]
The (.*)$ matches anything after username (including the slash) until the end.

.htaccess syntax code needing revision

1) i am trying to 301 all non www's to www's (including files)
e.g. /subdirectory/ + /subdirectory/1.jpg (all possibilities with the www.)
2) i am trying to exclude ONLY but ALL .html and .php files from showing and would like only the non trailing version to be indexed, the rest to be 301's to my domain
e.g. /example.html or .php >> /example
e.g. /example.html or .php >> to not work + to not be indexed
e.g. /example to ONLY work and to ONLY be indexed (to avoid duplicate content)
3) i am trying to 301 all dead links and 404's to my domain
e.g. /deadlink or /deadlink.pdf >> 301'd to my domain, example.com
Here is the code i currently have, however i am not sure if it's 100% proper.
Can someone please reply with a validated syntax for these 3 tasks? Thanks.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
OK, you've got three parts you're trying for here, and I'll address them all separately.
Before we begin, you only need to have one
RewriteEngine on
It only needs to be turned on once.
You should also set:
Options -MultiViews
as MultiViews can otherwise cause some weird issues here.
Redirect to WWW
Your current rules should work, but you can simplify them a little using the != prefix on RewriteCond:
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Removing .php and .html suffixes
You need two separate sets of rules here: one to remove suffixes from URLs, and another to readd them internally.
Let's do the second half first, since that's easier:
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
With this in place, though, removing suffixes is a little tricky without conflicting with the rule that goes ahead and tries to put them back on. The best approach I was able to come up with was:
RewriteCond %{THE_REQUEST} \.(html|php)
RewriteRule ^(.*)\.(html|php)$ $1 [R=301,L,NC]
But I'm open to suggestions.
301ing all dead links
Don't do that. If a link doesn't exist, it should return a page with a 404 status. If you want to redirect visitors to your home page from dead links, put a link in your 404 page.

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

mod_rewrite image problems

I currently have this in .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/(.*)/(.*)$ /type1/internal.php?parentFolders=$1&pageTitleID=$2 [L]
When I visit http://localhost/type1/page/home/play it works fine, however I would like to take the /page/ out therefore being http://localhost/type1/home/play
I have tried the following:
RewriteRule ^(.*)/(.*)$ /type1/internal.php?parentFolders=$1&pageTitleID=$2 [L]
however some images seem to vanish, and some don't.
Also, I find hard links in my nav do not work either as they are asking for variables used in internal pages.
I am not sure if this is a absolute/relative problem as my CSS is written as
<link href="http://localhost/type1/global.css" rel="stylesheet" type="text/css" />
and links seem to be full links when I inspect them with my browser!?
Any help would be great
As I understand the .htaccess file is located in /type1/ subfolder. Try these rules:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /type1/internal.php?parentFolders=$1&pageTitleID=$2 [L,QSA]
The (.*) pattern you are using is too broad for first parameter, I use ([^/]+) instead (any character except slash).
Added 2 rewrite conditions .. so only requests to non-existing files and folders will be rewritten. This should help with disappeared images (or may not -- it all depends how how the links are written).
Added QSA flag to preserve any existing query string.

Resources