I've successfully modified my .htaccess file to remove trailing slashes on most pages but I'm wondering how to exempt my home page/directory? For example:
domain.com/test/ successfully redirects to domain.com/test
HOWEVER, when I hit my domain it will append the root document
domain.com/ redirects to domain.com/index.php
Is there a condition that I can add to ignore root url trailing slash so that it doesn't attempt to remove the trailing slash and add my default script? Here's what I have so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
OK. After a bunch of trial and error I answered my own question.
The third line denotes that there has to be something in the URI in order to perform the redirect thus not redirecting if the url just contains the initial slash.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://www.domain.com/$1 [R=301,L]
Redirects request for all URLs ending in a / except for the root:
RedirectMatch 301 ^(.+)/$ $1
How about
RewriteEngine On
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1
Related
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 writing code in htaccess to add trailing slashes in url, also applying redirection.
My code is-
#Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
Redirect 301 /social-media-consultant http://example.com/seo-consultant/
Redirect 301 /uiux-developers http://example.com/graphics-designer/
Now, problem is that when I write code for adding slashes then redirection stops working.
Change order of your rules
Use only mod_rewrite based rules.
Have it this way:
RewriteEngine On
RewriteRule ^social-media-consultant$ http://example.com/seo-consultant/ [L,NC,NE,R=301]
RewriteRule ^uiux-developers$ http://example.com/graphics-designer/ [L,NC,NE,R=301]
#Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^ http://example.com/%{REQUEST_URI}/ [L,R=301,NE]
Make sure to clear your browser cache before testing this change.
I'm running apache on ubuntu 14.04 and trying to set up 301 redirects. Redirection is working, but not as expected. Here's what I have:
#REDIRECTS
Options +FollowSymLinks
RewriteEngine On
# REMOVES INDEX.PHP
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# REDIRECT SPECIFIC PAGES
Redirect 301 /main http://mikeheavers.com
Redirect 301 /main/ http://mikeheavers.com
Redirect 301 /main/code http://mikeheavers.com/tutorials.html
Redirect 301 /main/code/ http://mikeheavers.com/tutorials.html
The first two redirect 301s work, but the rest, such as /main/code, try to redirect to http://mikeheavers.com/code and note http://mikeheavers.com/tutorials.html. What am I doing wrong?
update: note that I need to be able to redirect urls both containing and not containing the trailing slash
Don't mix mod_rewrite rules with mod_alias that has Redirect directive. Use this:
#REDIRECTS
Options +FollowSymLinks
RewriteEngine On
# REDIRECT SPECIFIC PAGES
RewriteRule ^main/?$ http://mikeheavers.com [L,NC,R=301]
RewriteRule ^main/code/?$ http://mikeheavers.com/tutorials.html [L,NC,R=301]
# REMOVES INDEX.PHP
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Make sure to clear your browser cache before testing this change.
I am currently trying to do a 301 redirect for all the pages of a site i am working on. The problem is that this url:
http://site.com/cash/
http://site.com/credit/
and
http://site.com/cash
http://site.com/credit
display the same pages
This will result in a number of duplicate URL issues and start splitting your PageRank of our SEO.
I was trying to do that on my site where all the seo points to the non-slash version
BTW I have about 90 pages that i have to change...any ideas on a good way of achieving this
How do i do a 301 redirect in the htaccess to do this on all my pages
Edit:
So to understand correctly. this will do the 301 redirect
# Remove the trailing slash
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
But how does this exclude folder1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
and would i put that in the same htaccess file or create another htaccess file in the folder1 directory
You can add the following to your .htaccess to remove the trailing slash
# Remove the trailing slash
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
Update
You can then choose to also add www
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301]
or remove it
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Rather than using a 301, you could just use a rel canonical link.
You could use <shudder> mod_rewrite instead, if it's for an Apache server... It's disasterously inefficient but probably the best option if what you're bothered about is SEO
I have this htaccess:
RewriteEngine On
# redirect with www
RewriteCond %{HTTP_HOST} ^mydomain [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [R=301,L]
# add .php internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
So my .php files can be called without the .php extension.
But I'd like them to be called only with a trailing slash. So when this trailing slash is not given, it should be appended with a 301. The problem I have is that this is giving me problems with the initial www, and the .php extension itself (sometimes it is adding recursively .php).
How can it be done?
Thanks!
I think you need to add something like this before your last rewrite rule, to avoid rewriting URIs that already end in .php
RewriteCond %{REQUEST_URI} !\.php$