This has been bothering me for a while... I’ve noticed a strange thing occurring on our website..
When I type in a subpage URL without an ending “trailing slash”, it does not redirect or force the “trailing slash”.
I have added a “force trailing slash” script on my .htaccess and it still does not work. It works on other sites.
Here is the script:
/* # Force trailing slash
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
</IfModule> */
I have contacted GoDaddy and they are not sure why it isn’t working either. We do pay for firewall services (securi) with them so I'm not sure whether this is the culprit or not. Is this anything to even be concerned with related to SEO?
Here is a video demo I created. The first site is the one in question. The other site works:
https://www.loom.com/share/28d862ca0cf74a88a65e1ca1226afab4
Here is a page as an example (Notice no trailing slash)..
https://www.allvalleytransportation.com/phoenix-car-service
Here is a site where the script works just fine (again notice the typed URL below does not have a trailing slash"
https://koalabrands.com/eucalyptus-oil-balm-information
Related
I am having an issue where Google Webmaster Tools is reporting a ton of 404 links to my site which are coming from ask.com.
I have tried to get ask.com to fix their side but of course they are not, so now I am stuck with over 11k of bad links to my site which I am suspecting is effecting my ranks right now.
Anyways I have a possible way to 301 them, but not sure how to do it with .htaccess.
Here is the bad link pointing to my site
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/about.php
It should be
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/
Besides the about.php there are other variations of endings as well, I basically need to be able to remove the ending.
Problem is that the URL after /sentence-examples/ can change. The beginning is always:
http://www.freescrabbledictionary.com/sentence-examples/
So basically:
http://www.freescrabbledictionary.com/sentence-examples/<-keep but can change->/<-keep but can change->/<-remove this->
This .htaccess should be placed on the folder before sentence-examples:
RewriteEngine on
# Redirect /sentence-examples/anything/anything/remove to /sentence-examples/anything/anything/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(sentence-examples/[^/]+/[^/]+)/.* [NC]
RewriteRule ^ /%1/? [R=302,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /sentence-examples/examplesentence.php?havethis=$1&word=$2 [L]
Change 302 to 301 once you confirm it's working as expected.
If you have a CMS installed you might need a different rule to work along with it without conflicting.
Keep in mind that if you had previously tried different redirects using 301 aka permanent redirect its recommended that you use a different browser to test this rule to avoid the caching.
This is possibly quick and dirty but I've done a simple test on localhost and here just to make sure it works.
RewriteEngine On
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)\.php http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
You can see that I've added wildcard groups (.*) to the RewriteRule so that we can pick up the elements of the URL that we need to aid in proper redirection i.e. $1 and $2. You can also use the third one ($3) to get which destinations are being targeted alot for your SEO needs.
NB: The rule above assumes that that the redirected URL will always be from a .php target and to ensure that you can redirect regardless of whatever comes after the 3rd URL segment replace the RewriteRule with this
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)$ http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
I know very little about .htaccess files and mod-rewrite rules. Looking at my statcounter information today, I noticed that a visitor to my site entered a url as follows:
http://mywebsite.com/index.php/contact-us
Since there is no such folder or file on the website and no broken links on the site, I'm assuming this was a penetration attempt. What was displayed to the visitor was the output of the index.php file, but without benefit of the associated CSS layout.
I need to create a rewrite rule that will either remove the information after index.php (or any .php file), or perhaps more appropriately, insert a question mark (after the .php filename), so that any following garbage will be treated like a parameter (and will be gracefully ignored if no parameters are required).
Thank you for any assistance.
If you're only expecting real directories and real files that do exist, then you can add this to an .htaccess file. What it does is it takes a non-existent file or directory request and gives the user the index.php page with the original request as a query string. [QSA] appends any existing query string.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?$1 [PT,QSA]
I found a solution, using information provided by AbsoluteZero as well as other threads that popped up on the right side of the screen as the solution came closer.
Here's the code that worked for me...
Options -Multiviews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectorySlash Off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# translate PATH_INFO information into a parameter
RewriteRule ^(.*)\.php(\/)(.*) $1.php?$3 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
I got the removal of trailing slash from another post on StackOverflow. However, even after removing the trailing slash, the rewrite rule did not account for someone appending what looks to be valid information after the .php file
(For example: mysite.com/index.php/somethingelse)
My goal was to either remove the "/somethingelse", or render it harmless. The PATH_INFO rule locates a "/" after the .php file, and turns everything else from that point forward into a query string (which will usually be ignored by the PHP file).
I'm very, very new to mod rewrite rules, and having trouble with one I'm using to redirect a site previously hosted in a subdirectory to a new domain. Here's the rule I'm using:
RewriteEngine on
RewriteBase /oldsite/ #also tried this without the trailing slash
RewriteRule ^.* http://newsite.com [R=301,NC,L]
This works perfectly until you get 3 levels deep and add a trailing slash to the redirected url. So, the results look like this:
olddomain.com/oldsite redirects to newsite.com [CORRECT]
olddomain.com/oldsite/ redirects to newsite.com [CORRECT]
olddomain.com/oldsite/subdirectory redirects to newsite.com/subdirectory [CORRECT]
olddomain.com/oldsite/subdirectory/ redirects to newsite.com [INCORRECT!]
I feel like I'm 99% there, but pulling my hair out a bit figuring out that last little bit. Any idea what I need to change?
Thanks!
Also, I tested placing the .htaccess file in the public html folder as well as in the /oldsite folder and there are no other htaccess files elsewhere on the site.
You're almost there, put this in the .htaccess in /oldsite on your server:
RewriteEngine On
RewriteRule (^.*) http://newsite.com/$1 [R=301,NC,L]
When you put the regex to match in parenthesis, it becomes available to use as $1. If you have more than one, $1 is the first, $2 the second, etc.
I suggest reading the manual page for mod_rewrite, specifically the rewriterule section (http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule).
I have a website where if I go to the URL http://mysite.com/community it shows page not found. But, the URL http://mysite.com/community/ correctly displays the page. How can I set up a rewrite for that "/" after community?
This is my present .htaccess:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^admin$ Admin/index.php?qstr=$1 [L]
RewriteRule ^(.*)/$ index.php?qstr=$1 [L]
These were the ones tried by me, but failed
First,
RewriteRule ^(.*)/community $1/community/ [L]
second,
RewriteRule /community /community/ [L]
All with different combinations of with and without [L].
From the Apache URL Rewrite Guide:
Trailing Slash Problem
Description:
Every webmaster can sing a song about the problem of the trailing slash on URLs referencing directories. If they are missing, the server dumps an error, because if you say /~quux/foo instead of /~quux/foo/ then the server searches for a file named foo. And because this file is a directory it complains. Actually it tries to fix it itself in most of the cases, but sometimes this mechanism need to be emulated by you. For instance after you have done a lot of complicated URL rewritings to CGI scripts etc.
Solution:
The solution to this subtle problem is to let the server add the trailing slash automatically. To do this correctly we have to use an external redirect, so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. For instance, a request for image.gif in /~quux/foo/index.html would become /~quux/image.gif without the external redirect!
So, to do this trick we write:
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo$ foo/ [R]
The crazy and lazy can even do the following in the top-level .htaccess file of their homedir. But notice that this creates some processing overhead.
RewriteEngine on
RewriteBase /~quux/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
Well, after trying out all the above solutions as well as some of my own, I finally solved this. I'm definitely sure that this is NOT a complete solution but it sure solved it for the time being.
Solution: Just created an empty directory named "community" in the root folder. That's it!
But I'm still on the lookout for the actual solution to this.
I'm a little stuck with with my .htaccess redirect.
It was working find while I was with PHP4 but the recent move to a new host with PHP5 have changed things for which I've no clue.
I'm working on a URL shortening service. Here, for a URL like http://example.com/e72b0f, it gives me http://example.com/forward.php?e72b0f
Earlier with my .htaccess file, the "forward.php?" was masked (hidden). How can I bring back this behavior. Here is the .htaccess for your reference.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \/([0-9a-z]{6})$ [NC]
RewriteRule ^(.*) http://example.com/forward.php?%1 [L]
Btw, I also do not rule out the issue being in the PHP Script. The developer that did it for me is too busy to look at it.
If you rewrite to an http:// URL that the server doesn't think is in the site, mod_rewrite will do a redirect instead of just a rewrite. In order to see if this is happening, make a page that has nothing in it but
<?php echo $_SERVER['SERVER_NAME']; ?>
and see if says it's going to "nsfw.in".
Either way, you should be able to strip off the http:// nsfw.in from the beginning of the URL and just rewrite it to /forward.php?%1. You may need to add a PT flag in order for it to be interpreted as a URL and not a FS path.