I am using this code for my redirects on my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^allvalleytransportation.com [NC]
RewriteRule ^(.*)$ https://www.allvalleytransportation.com/$1 [L,R=301,NC]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Force trailing slash
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
</IfModule>
It appears to resolve the "/" on my subpages, however my index page does not force a trailing slash.
If I type in allvalleytransportation.com and copy the URL from the browser it generates this:
https://www.allvalleytransportation.com/
This is correct, however on the browser window itself, it appears without the trailing slash. Please see screenshot below. Is this something to be concerned about or is this normal?
Here is a conversation I had with YOAST that may help explain further.
https://wordpress.org/support/topic/canonical-issue-15/#post-15921609
An absolute URL with no path is the same as one with the path /.
If you type http://example.com or http://example.com/ into a browser's address bar, it will ask for /.
The difference is only in what is displayed in the browser's address bar.
(The same applies to any other client, only it might not have a visible address bar).
Related
RewriteEngine On
RewriteRule ^$ /index.phtml [R,L]
RewriteRule ^index.phtml.+$ /index.php$1 [R,L]
I have 2 urls like https://www.example.com/index.phtml and https://www.example.com/index.php which both point to very different sites. I want to redirect to index.php in case there are any parameters found after index.phtml.
For example, https://www.example.com/index.phtml?a=b should go to https://www.example.com/index.php?a=b (second rule). I am trying the above rules but the second one isn't matching (the first rule works fine). Thanks for any help/suggestions
Please ask if you need any more details.
You may use this code in site root .htaccess:
RewriteEngine On
# if there is a query string then redirect
# /index.phtml to /index.php
RewriteCond %{QUERY_STRING} .
RewriteRule ^(?:index\.phtml)?$ /index.php [L,NC]
# redirect landing page to /index.phtml
RewriteRule ^$ /index.phtml [R=302,L]
Note that QUERY_STRING get automatically appended to target URL so /index.phtml?a=b will be redirected to /index.php?a=b.
Make sure to clear your browser cache before testing this change.
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your redirect rules.
With your shown samples, please try following .htaccess rules file. Please make sure that your htaccess file and index.php are present in same folder.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##For without query string rules.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.phtml/?$ index.php [R=301,NC,L]
##For query string rules.
RewriteCond %{THE_REQUEST} \s.index\.phtml\?([^=]*=\S+)\s [NC]
RewriteRule ^ index\.php?%1 [R=301,L]
The URLs
https://example.com/moved/
https://www.example.com/moved/
should be redirected to the URL
https://my.example.com/#views/settings.php?id=2
via .htaccess.
This is what I have tried:
RewriteCond %{HTTP_HOST} ^example.com
RewriteCond %{REQUEST_URI} moved$
RewriteRule (.*)$ https://my.example.com/#views/settings.php?id=2 [R=301,L]
However it seems not to work. I guess the reason is the #hashtag within the URL where I want to redirect to (it marks a comment within .htaccess).
How to redirect this correctly?
Change your rule to this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^moved/?$ https://my.example.com/#views/settings.php?id=2 [R=301,L,NE,NC]
^(?:www\.)?example\.com$ will match www.example.com and example.com
Flag NE is used for no escaping of # in redirected URL
Pattern moved/?$ matches /moved/ and /moved, thus making trailing slash optional.
Make sure to clear your browser cache or use a new browser for testing.
I want to remove the www. from the front of my URL so I added the following code to .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
Which worked fine, but when people go to www.example.com/example it redirects them to the homepage.
Is there any way for it to just remove the www. from the page they are looking for then redirect them to that page instead of the homepage?
Just add $1 to the URL you redirect to, to capture the bracketed part in RewriteRule ^(.*)$ (i.e. the trailing example). I think something like this should do it:
RewriteRule ^(.*)$ http://%1$1 [R=301,QSA,NC,L]
I have an strange situation with .htaccess configuration.
I have a multidomain hosting, with one directory per each domain.
For example, I have:
http://www.domain1.com
http://www.domain2.com
and the following directory structure:
/domain1/
/domain1/index.php
/domain2/
/domain2/index.php
/domain2/subdirectory/
/domain2/subdirectory/index.php
and a .htaccess file with the following content:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain1/.*$
RewriteRule ^(.*)$ domain1/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain2/.*$
RewriteRule ^(.*)$ domain2/$1 [L]
all works as I like with one exception:
if I do:
http://www.domain2.com/subdirectory/
(with a slash at the end)
the content is showed propertly and the url on the address bar keep unchanged, but
if I do:
http://www.domain2.com/subdirectory
(without a slash at the end)
the content is showed properly but the url on the address bar is changed to:
http://www.domain2.com/domain2/subdirectory/
(notice the subdirectory name and a slash is appended at the end)
and I don't like the user see that change on the address bar, that's my problem
This is mod_dir and DirectorySlash interferring with the URI-file mapping pipeline. By default DirectorySlash is turned on, which makes it so when apache thinks a request is for a directory, and it's missing the trailing slash, then it 301 redirects to the URI with the trailing slash. There's a good reason for doing this because of a possible information disclosure security concern. If you don't allow for directory indexing, you could just turn it off:
DirectorySlash Off
And then Apache won't redirect to the trailing slash at all. Or you can do the redirecting yourself as part of the rewrite engine. The reason why your internally rewritten URL is exposed is because 2 modules are mangling the URI, mod_dir and mod_rewrite, and they're doing it without any awareness of each other. So the URI gets rewritten by mod_rewrite and then mod_dir comes along and says "this is a directory!", mangles the URI some more by adding a trailing slash and flags it for a redirect response. Eventually, what you get is a redirect with a mangled URI. By handling the redirect from within mod_rewrite, by the time it gets to mod_dir, everything's peachy.
These would need to be above your current rules
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain1/.*$
RewriteCond %{DOCUMENT_ROOT}/domain1%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain2/.*$
RewriteCond %{DOCUMENT_ROOT}/domain2%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
I have searched around for a while and had a go at tweaking this file myself and I'm almost there but there is one case which I can't figure out...
How to get both a www. AND a forward slash at the same time
If I type in spectrl.com, it redirects to www.spectrl.com CORRECT - Adds www.
If I type in www.spectrl.com/ebaycalculator it redirects to www.spectrl.com/ebaycalculator/ CORRECT - Adds /
But if I type in spectrl.com/ebaycalculator I get a 404 error when it should go to www.spectrl.com/ebaycalculator/
Here's my .htcaccess file, kept at the root:
RewriteBase /
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://spectrl.com/$1/ [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thanks
#Kavi
Try this:
RewriteEngine On
RewriteCond "%{HTTP_HOST}" "^(?:www\.)?(.*)" [NC]
RewriteCond "%{REQUEST_URI}" "!/$"
RewriteRule "(.*)" "http://www.%1%/$1/" [R=301,L]
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteRule "(.*)" "http://www.%1/$1" [R=301,L]
The first RewriteCond captures the hostname (without any leading www.) in the reference %1. That condition will always succeed.
The second RewriteCond checks for the trailing slash; if not found, the next RewriteRule will be triggered.
That first RewriteRule uses the captured www.-less host name to construct a redirect that includes www. and the training /.
The second stanza will be triggered if the request falls through because it does have a trailing /. It checks for a leading www., and does the same sort of redirect (only without appending a slash, since there's already one there) as the first stanza.
At least, that's how is should work; I haven't tested it. :-)
After removing and re-uploading .htaccess and then clearing the cache, everything seems to be working as intended using my original code in the question.
Hope this will be helpful for someone else.