for our client project we need to redirect all url calls to the matching .php file extension. this is done for SEO (google) ranking. avoiding indexing both url (with *.php and without)
we try to do this using the .htaccess file (shared host) but it seemed to only work for redirects that have a different url and not to the one that just adding the ".php" extension.
different url works
abc -> abc2.php
same url doesnt work
abc -> abc.php
here is our code sample:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co.uk
RewriteRule (.*) http://www.abc.co.uk/$1 [R=301,L]
# this doesn't work
Redirect 301 /test http://www.abc.co.uk/test.php
# this redirect works as the url is different
Redirect 301 /test-abc http://www.abc.co.uk/abc.php
we host with 1&1 shared host server.
php version is 5.5
I also tried this code that redirects but the page not loading now:
RewriteRule ^(.+)\.(.*)$ http://abc.co.uk/$1.php [R,NC]
also noticed url without file extension (.*|html) will not redirect
Try this .htaccess in DocumentRoot:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co\.uk$ [NC]
RewriteRule (.*) http://www.abc.co.uk/$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L,R=302]
This appears to be a 302 re-direct, which is not SEO freindly. I tested the above and it doesnt work, also, if it did I wonder what would happen to url's that did not require the .php extension on the URL.
Basically we're trying to get a 301 re-direct working, but it seems to be conflicting with the rules set up that contain the same url. (IE abc > abc.php)
I wonder if there could be some kind of Apache setting or even and unlikely a PHP setting? the 301's work on any other linux server that I try.
Related
We recently moved our web site to another server, say http://newdomain.com, while our old site is at say http://olddomain.com
I would like to keep the users directory on the old site accessible as it was before, that is http://oldomain.com/~user, but anything accessible under the root folder point to the new domain. The users directory on the old server are in the /home/user file system. So, I wrote following rules:
RedirectMatch permanent ^/~(.*) /home/~$1
RedirectMatch permanent ^/(.*) http://newdomain.com/$1
while the second rule works flawlessly, the first one still wants to map to the new site as following: http://newdomain.com/~user.
How can I fix the two rules so that anything in /var/www/html on the old site redirects to the new site, but anything under /home does not redirect?
--
Here is the new code based on Ben's solution, which maps the urls for the access to users' home pages correctly, but the browser complains and does not show their sites. The root folder urls redirection works fine.
RewriteEngine on
# RewriteBase / -- I had to comment this as the apache did not like it
Rewriterule ^/~(.*) /~$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/home/~
RewriteRule ^/(.*) http://newdomain.com/$1 [R=301,L]
You should use RewriteCond to test if the URI prefix with /home/~, and use RewriteRule to rewrite the URL and make a permanent redirect.
RewriteEngine On
RewriteBase /
RewriteRule ^~(.*) /home/~$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/home/~
RewriteRule ^(.*) http://newdomain.com/$1 [R=301,L]
Line 3: if URI prefix with ~{any_characters}, rewrite the URI to /home/~{any_characters}, redirect permanently
Line 5: Test if the URI not prefix with /home/~{any_characters}
Line 6: Then process the rewrite rule, rewrite the URI from {any_characters} to http://newdomain.com/{any_characters}, redirect permanently
I'm having trouble making sense of this .htaccess file I've inherited at my new job. I'm a graphic designer / marketer learning web development. Our site has a bunch of duplication errors and I want to rename our links and have them 301 redirected.
This is the first part of the .htaccess file:
RewriteEngine Off
RewriteCond %{HTTP_HOST} !^blog.asseenontvhot10.com$ [NC]
RewriteCond %{REQUEST_URI} !/sitemap\.xml
AddType text/html .shtml, .html, .htm
AddHandler server-parsed .shtml, .html, .htm
AddHandler application/x-httpd-php5 .html .htm .shtml
#Options FollowSymLinks Includes -Indexes
From what I understand the first part links our blog to the main site, and I'm not sure what the next line does.
The addtype line and the ones that follow enable .htaccess to read/write those filetypes?
Here is the part that I believe is relevant and what I need to change:
RewriteEngine on
rewritecond %{http_host} ^[^.]+\.[^.]+$ [nc]
rewriterule ^(.*)$ http://www.%{http_host}/$1 [r=301,nc]
RewriteRule default.html /home.html
RewriteRule contact\.html /cgi-bin/commerce.cgi?display=contact
RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteRule register\.html /cgi-bin/commerce.cgi?register=action
RewriteRule products\.html /cgi-bin/commerce.cgi?listcategories
RewriteRule parent_category/(.*)/(.*)/ /cgi-bin/commerce.cgi?listcategories=action&parent=$1
RewriteRule category/(.*)/(.*)/(.*)\.html /cgi-bin/commerce.cgi?search=action&category=$1&page=$3
RewriteRule category/(.*)/(.*)/ /cgi-bin/commerce.cgi?search=action&category=$1
RewriteRule category/(.*).html /cgi-bin/commerce.cgi?search=action&page=$1
RewriteRule product/(.*)/(.*)/ /cgi-bin/commerce.cgi?preadd=action&key=$1
RewriteRule basket\.html /cgi-bin/commerce.cgi?display=basket
RewriteRule log-in\.html /cgi-bin/commerce.cgi?login
RewriteRule home\.html /cgi-bin/commerce.cgi?display=home
RewriteRule search\.html /cgi-bin/commerce.cgi?display=search
RewriteRule all-items\.html /cgi-bin/commerce.cgi?search
RewriteRule update_user\.html /cgi-bin/commerce.cgi?displayuser=action
RewriteRule order_history\.html /cgi-bin/commerce.cgi?orderhistory
RewriteRule logout\.html /cgi-bin/commerce.cgi?logout
RewriteRule specials\.html /cgi-bin/commerce.cgi?search=action&keywords=nav_specials
I know the first part forces www. and does a 301 redirect, even though I haven't seen this way of accomplishing it on any guide I've read. The first thing I really need to change is making the home page 301 redirect from:
http://www.asseenontvhot10.com/cgi-bin/commerce.cgi?display=home
to
http://www.asseenontvhot10.com/
So far as I can tell the RewriteRule makes this an option but dosn't force it. Will adding [r=301] to the end of the rewriterule work?
I've also read that there is code you need to add for .cgi to work as .php does but I'm totally lost there.
Update:
I tried adding this line to the bottom -
Redirect 301 /cgi-bin/commerce.cgi?display=home /
and I got a 500 error.
2nd Update:
I added this code:
RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteCond %{QUERY_STRING} ^display=about$
RewriteRule ^cgi-bin/commerce\.cgi$ /about [R=301]
And it 404'd. When I replaced the .htaccess file with the back up and the about page still 404's... Now I'm really at a loss and don't know how to revert the site. I don't even know where the redirect would be stored outside of the htaccess file once it's reverted.
3rd Update: (making progress)
I added this code:
RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteCond %{QUERY_STRING} ^display=about$
RewriteRule ^cgi-bin/commerce\.cgi$ /about\.html? [R=301]
and I got a redirect loop. It was finally redirecting to about.html which I thought the first rule renamed /cgi-bin/commerce.cgi?display=about to, but it redirected to it over and over again.
The Redirect directive in Apache requires that the redirect destination be a fully qualified URL with a scheme and a URL. So this would work:
Redirect 301 /cgi-bin/commerce.cgi http://www.yourdomain.com/
However, I don't believe that Redirect can check the query string, so in order to only apply this redirect if the query string is "display=home" you will probably have to use mod_rewrite like this:
RewriteCond %{QUERY_STRING} ^display=home$
RewriteRule ^cgi-bin/commerce\.cgi$ / [R=301]
The RewriteCond checks that the query string exactly equals "display=home" and the RewriteRule will only trigger if this condition is met. And in the RewriteRule the [R=301] flag instructs mod_rewrite to redirect the visitor's web browser to the target path, / in this case.
By the way, looking at the existing conditions in your .htaccess file, I can't see how your users are seeing the /cgi-bin/commerce.cgi path in their web browser. All of those RewriteRule directives are for silent rewrites. This means that the user will continue to see the "friendly" URL in their web browser, but in the background Apache will actually find the file located at the /cgi-bin/commerce.cgi path and return the content or result of that file to the user. So your users should already be seeing the friendly version of the URL. If what you are trying to do instead is silently rewrite a request for "/" (site root) so that Apache silently rewrites the path and actually fetches the content in the real file /cgi-bin/commerce.cgi?display=home then you probably want this instead:
RewriteRule ^$ /cgi-bin/commerce.cgi?display=home
This will only match a request for the root directory (with no filename specified) and will silently fetch and return the content which results from calling the commerce.cgi file with a "display=home" parameter.
I'm using a php app called Yourls. It's a self-hosted url shortener and it's pretty great, I'm happy with its overall functionality. Due to the nature of its development however there isn't much in the way of support. Let's pretend the base url is af.to, where a shortened url would be af.to/goo that might redirect to whatever url is defined by 'goo'. The problem I'm facing is that if someone goes to af.to, they end up on a 403-Forbidden. I'd rather the client is redirected to a specific url instead. I have already picked up a plugin for Yourls which redirects to a url when a shortlink is not found or mis-typed, but this does not cover the base of af.to
I attempted to put in a 403 redirect in the .htaccess, but that broke the whole yourls script resulting in a 500 server error.
Current .htaccess looks like this:
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
Any help on what I need to do?
Thank you.
The RewriteCond blocks tell the RewriteRule to skip existing files / folders. When you go to http://af.to/, the root folder exists : no redirection. The apache server doesn't find any index.html (or index.php) file, isn't allowed to list the content of the folder, give up and returns a 403 Forbidden.
You can create the index.html file to show some content or you can add these lines to redirect to an other url :
# just after RewriteEngine On
RewriteRule ^/$ http://my-compagny.com/ [L,R=301]
So I've been tinkering. Apache's .htaccess was redirecting my WWW client just fine (if there is no WWW present, 301 to the same URL with a WWW included), and it was redirecting things with file extensions (i.e. index.html will redirect to index.php). It's only URLs that are without an extension. It refuses to decipher them.
My current file:
RewriteEngine On
RewriteBase /
# Clean URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ $1.php [NS,L,R=permanent]
# Force WWW
RewriteCond %{HTTP_HOST} !^www\.thevgc\.net$
RewriteRule ^(.*)/?$ http://www.thevgc.net/$1 [NS,L,R=permanent]
I've been trying to figure this out all night long now. mod_rewrite is enabled, and I had to create a symbolic link to /etc/apache2/mods-enabled/rewrite.conf before actually making the file, which consists of the following:
RewriteEngine On
Thoughts on this dilemma of mine?
SO! I found the answer to my problem. Looks like if you run the command a2dismod negotiation it magically starts working, or at least, it did in my case. Problem solved, case closed, thread done. The mystery has been solved.
I'm trying to serve CSS files for my site from a separate domain.
I've got the following entry in my site's .htaccess (in the document root):
RewriteRule ^templates/*\.css$ http://css.mysite.com/templates/$1 [R=301,L]
With the intention of simply matching:
http://www.mysite.com/templates/default/styles/main.css
… and sending an HTTP 301 redirect to:
http://css.mysite.com/templates/default/styles/main.css
But what's actually getting called is:
http://css.mysite.com/templates/.css
(I'd like to duplicate the above for *.js and serve them from js.mysite.com and all png & jpgs and serve them from img.mysite.com.)
Any insights would be fantastic. I've got to admit, htaccess rewrite rules always seem to elude me.
Try this and let me know:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^css\.
RewriteRule ^templates/(.*)\.css$ http://css.mysite.com/templates/$1.css [R=301,L]
or a more "dynamic way"
RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)\.(js|css)$ http://$2.mysite.com/templates/$1.$2 [R=301,L]
RewriteRule ^templates/(.*)\.(jpg|gif|swf|jpeg|png)$ http://imgs.mysite.com/templates/$1.$2 [R=301,L]
This one will check for JS and CSS and use the subdomain based on the file extension: so .js will go to js.mysite.com and .css will go to css.mysite.com and same for images but these goes to imgs.mysite.com instead.
or be even more generic:
RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)$ http://media.mysite.com/templates/$1 [R=301,L]
You redirecting everything that start with templates