I have a simple file mydomain.com/business_nottingham.html
and i want to re-write that to an SEO friendly URL, eg mydomain.com/business-nottingham/
I've googled all the examples but they seem to be designed for either CMSes or PHP scripts.
Is there a simple .htaccess re-write example available that allows me to do something very simple as above?
Edit: I managed to find the following code finally
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
However, it's still not quite working. If i enter the tidy URL, i get re-directed to a 404 page, but putting in the exact .html file name gets me to the right file but doesnt present me with a clean url.
I've tried various combinations of the above by reading various articles and tutorials but for some reason it doesn't seem to work for me.
You want to redirect only when the actual request is for an .html file, then you want to internally rewrite to the html file. The way URLs resolve is the browser shows where it thinks it's going (URL in the address bar), then the request is made to the web server. If the webserver (where the htaccess file is) wants to change what's in the browser's URL address bar, it needs to tell the browser to literally load an entirely different URL. The browser will then request the new URL. Then the server must internally rewrite that URL back to where the actual resource is (the first URL), but the browser doesn't see this happen.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.html
RewriteRule ^ /%2/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^ /%1.html [L]
Related
Google discovered that I'm allowing end users to navigate my content using ajax loading, and is loading my pages as a user client rather than requesting them as new page loads. So instead of trying to index www.mysite.com/page, it's requesting www.mysite.com/?_escaped_fragment_=/page
Which is not at all what I want it to do. My snapshots are served at the same URL as the ajax-loaded content. The site is not using queries, it's not supporting them and I don't want to build that support. This means that all the pages look broken to google which of course is unfortunate!
Currently all page requests are redirected server side using .htaccess sending requests to the index.php file which in turn compiles the html doc on the server before serving to the client. The site serves perfectly valid and unique html documents for all pages. But google insists on doing it the ajax way and adding the query which always returns a broken page.
I'm not a .htaccess expert, but it seems to me that the easiest way to solve this would be to rewrite the request, remove the ?_escaped_fragment_=/ bit and permanently redirect any such requests to what currently works which is to load the pages using their correct url's.
Anyone know how I would go about doing that? Below is the current redirect part of my .htaccess file which needs to be amended with the _escaped_fragment_ stripping code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
#if trailing / remove it with a permanent redirect
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
#if missing www. add it with a permanent redirect
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
#requests for index.php never rewritten
RewriteRule ^index\.php$ - [L]
#if file or directory are missing, route to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This is how I rewrote it so that all ?_escaped_fragment_=/XXXXX requests got redirected to /XXXXX without the query
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%1? [L,R=301]
This makes www.domain.com/?_escaped_fragment_=/somepage redirect (permanently) to www.domain.com/somepage
...which is just what I wanted.
Sorry for the somewhat cryptic title! I'll try to explain. I have the following in my .htacess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
So users can type domain.com/foo/bar and domain.com/foo/bar.php is served. Of course, typing domain.com/foo/bar.php also loads the same page.
So, in Google Webmaster Tools (under HTML Suggestions), Google thinks that i have duplicated all my meta descriptions and title tags as it sees bar and bar.php as two different files. I am worried Google will penalise this "duplicate content". What should i do? I am sure im not the first one to have this issue, but i am struggling to find related questions/answers.
Thanks for any help
Use [R] (the redirect flag).
RewriteRule ^(.*)$ $1.php [R,L]
This forces an external redirect, so users who attempt to access domain.com/foo/bar.php will be redirected (through HTTP 302 Found) to domain.com/foo/bar. You'll also want to use [L] (last rule), so that the redirect happens immediately.
The answer i used (as per my original comment) from here which solved my problem
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php?\
HTTP RewriteRule (.+)\.php?$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(([^/]*/)*[^/.]+)$ /$1.php [L]
I'm building an Ajax site that runs off of a root-level index.html file and uses history.js for pushState/popState, which I have updating the urls such that they are nice and clean without hashes or bangs (Example: site.com/section/1).
How can I do a mod_rewrite so that when a user tries to link to site.com/section/1 or site.com/section (or anywhere other than the root), the server serves up site.com/index.html?
From there the js would load the requested content in the url via ajax.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.html [L]
If you want to do something with the actual request (the /section/1 part) you can access it via $1. Example:
RewriteRule (.*) /index.html?path=$1 [L]
Which will rewrite /section/1 to /index.html?path=/section/1
I've been working on creating seo friendly urls for my site and have done this successfully with the following rules:
RewriteEngine on
RewriteBase /
#redirect non www to www
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
# Redirect any request with page=var to /var/ format
RewriteCond %{QUERY_STRING} ^page=(.+[^/])$ [NC]
RewriteRule ^index\.cfm$ http://%{HTTP_HOST}/%1/? [R=301,L,NC]
# If not an existing file or directory rewrite any request
# to page var.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ index.cfm?page=$1 [QSA,L]
My problem is that because of the last rule (I think), any root dir request is made into a friendly url such as /pagethatdoesntpointanywhere/ whether it points anywhere or not. Now I'd be happy with a 404 but it's not doing that it's just displaying the homepage.
I've also tried adding a blanket 404 rule:
# 404 files that don't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ /notfound.html [R=404,L,NC]
But that makes all of the friendly urls 404s as well.
Could someone explain where I am going wrong here please?
Use ErrorDocument 404 /notfound.cfm to display not found error page when Apache cannot find the file (replace notfound.cfm by your file).
In index.cfm, if value of page parameter is unknown (e.g. pagethatdoesntpointanywhere), display 404 error page (use the same/similar code as notfound.cfm). It is the right place to do considering your rewrite rules and the fact that you checking which page to display here anyway. Lots of products/frameworks work in similar fashion (for example: WordPress).
Use both #1 and #2. Number #2 will work for 1-folder deep URLs (e.g /meow/) while #1 will catch any other URLs (e.f. /meow/kitten/ or /meow/wuf/oink.css).
I'm looking for a way to rewrite URLs only if the path doesn't exist. This isn't to handle 404s, but to redirect page URLs to a shared PHP file (ie: '/contact-us/' -> '/show_page.php?page=contact-us').
The basic redirect is easy enough to achieve, however I want to be able to override the default page by adding /contact-us/index.php in the site root.
Is this achievable with mod_rewrite or would I have to do something else?
Just check the value of the REQUEST_FILENAME variable:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ...