I have setup my .htaccess to add a trailing slash to each URL, but having done this I can see that my Google Analytics conversions (goal type is 'Destination') do not work.
The page I'm trying to track is:
/thank-you/contact/
And the trailing slash gets added by .htaccess if not already there. In GA I have told it to track 'Begins with' /thank-you/contact, 'Equals /thank-you/contact/' etc. Nothing works.
If I comment out the .htaccess rule that adds the trailing slash the conversion tracking immediately starts working again. Have I got some kind of bad config in my .htaccess?
RewriteEngine On
# add trailing slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.co.uk/$1/ [L,R=301]
#remove www
RewriteCond %{HTTP_HOST} ^www.mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://mydomain.co.uk/$1 [R=301,L]
#remove index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Try regular expression condition with goal settings
thank-you\/contact.*
trailing .* would match or zero and unlimited characters
Related
I have a .htaccess rule that adds trailing slash if the request is without it.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
I want to suspend the rule for the file sitemap.xml. That means I want to keep the adding slash rule for every request but sitemap.xml file.
Is it possible?
I've modified my .htaccess file to force trailing slashes on all the pages but I'm wondering how to remove the trailing slash in a particular url page? Need help. For example:
abc.com/test successfully redirects to abc.com/test/
But I want to remove that force trailing slash in a particular url of the site,
abc.com/demo/ should redirect to abc.com/demo
Here what I have done so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/demo/
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
Update:
To remove trailing slashes for multiple urls what would be the code, eg abc.com/demo2/ abc.com/demo3/ abc.com/demo4/ etc.. Any suggestions?
Try :
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/demo/ [NC]
RewriteRule ^demo/$ /demo [L,R]
RewriteCond %{REQUEST_URI} !^/demo [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
I am amending my htaccess file to achieve non-www to www (this is working) plus removing the trailing slash at the end of the URL, e.g.:
www.domain.bc.ca/club/ ---> www.domain.bc.ca/club
www.domain.bc.ca/club/index.html/ ---> www.domain.bc.ca/club/index.html
The portion of the htaccess file is below - the Force www bit is working; the Remove trailing slash bit is not. Help! Many thanks, Amanda.
# Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.bc\.ca$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
I'm going to go out on a limb and guess that you're trying to access a directory when this happens. In your example, the "club" seems to be a directory and when you redirect /club/ to /club, a module called mod_dir will redirect it back to having the trailing slash again. There's a really good reason for this, because if the trailing slash is missing for a directory, the directory's contents will be displayed instead of the index file. That means if you were able to go to www.domain.com/club (without the trailing slash), you'd see all the contents of the club directory instead of the club/index.html file.
If that's ok with you, then you can turn off mod_dir by adding this to your htaccess file:
DirectorySlash Off
But then you'd need to internally add the slash back in:
DirectorySlash Off
# Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.bc\.ca$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
# Add the slash back
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ /$1/ [L]
I'm having a hard time having this to work..
I have installed YOURLS wich is a PHP script to shorten urls.
In order to work, it needs to have this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourls-loader.php [L]
No problem here.
But I also want to use a directory for image hosting that has nothing to do with the PHP script.
It would check if the requested url ends with .jpg|.jpeg|.gif|.png and RewriteRule would redirect to /imgshare/$1
I've tried the code below but I get a server error when going to mysite.com/img.jpg but not for the url redirection "mysite.com/y4Jd":
RewriteCond %{REQUEST_URI} !(\.jpg|\.jpeg|\.gif|\.png)$ [NC]
RewriteRule ^(.*)$ /yourls-loader.php [L]
RewriteCond %{REQUEST_URI} (\.jpg|\.jpeg|\.gif|\.png)$ [NC]
RewriteRule ^(.*\.(jpeg|jpg|png|gif))$ /imgshare/$1 [L]
This is not the issue, but as a note, your second RewriteCond already matches files with image endings, so there's no need to repeat that match in the RewriteRule. Alternately, there's no need for the RewriteCond since it's redundant to the RewriteRule.
The real issue, however, may be that you have an extra slash in the final rule. $1 will contain the leading slash matched from the original URL so your rule is currently adding two slashes between imgshare and the file name. I would implement the rule like this:
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|gif|png)$ [NC]
RewriteRule ^(.*)$ /imgshare$1 [L]
I've got the following rewrite rules in my .htaccess, the first 4 lines are supposed to deal with allowing to access the site without index.php, and works fine, until I add the last bit which I'm trying to use to remove trailing slashes from the sites URLs.
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(uploads|cache|themes|default|admin\.php|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1
# Remove trailing slashes
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
When I add the last line, and I visit the root of my site, the index.php part gets appended to the URL, why is this?
When I add the last line, and I visit the root of my site, the index.php part gets appended to the URL, why is this?
This is because the rules are applied sequentially. You want the redirect to happen before you route stuff to /index.php. Just swap those rules around:
# Remove trailing slashes
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(uploads|cache|themes|default|admin\.php|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
Following rules are work for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#Removing trailing slash
RewriteRule ^(.*)/$ /$1 [L,R]
#Removing index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=302,NE,L]