Rewrite a dynamic url after extension removed - .htaccess

I've been working on my HTAccess for a couple days now, and I've hit a dead end.
I've rewritten and redirected the files to be extensionless, now I need to rewrite the url to be SEO Friendly.
Previously, the URL was: http://www.example.com/member.php?playername=encodedName
I removed the extension: http://www.example.com/member?playername=encodedName
I can't quite get it to: http://www.example.com/member/encodedName
Here's what I've gotten so far:
Code trying to redirect to SEO Friendly URL. Does NOT work.
RewriteRule ^member/([^/]*)$ /member.php?playername=$1 [L]
RewriteRule ^squad/([^/]*)$ /squad.php?squadname=$1 [L]
RewriteRule ^article/([^/]*)$ /article.php?articlename=$1 [L]
Unless directory, remove trailing slash. Works.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
Resolve .php file for extensionless php urls. Works.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Redirect external .php requests to extensionless url. Works.
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php $1 [R=301,L]
Does anyone have an idea as to what I am doing wrong? It's probably a very newbie problem that I just haven't encountered yet.

The "Code trying to redirect to SEO Friendly URL" part looks fine. And while you're redirecting requests made directly to php files to remove the extension, you're not doing anything specifically about the 3 SEO friendly URLs that you are rewriting back.
If when you go to http://www.example.com/member/encodedName, and you're not being served the content at http://www.example.com/member.php?playername=encodedName, then this looks like a Multiviews problem. You'll need to turn off Multiviews either in your server/vhost config or add this to the top of the htaccess file (or in the appropriate already existing Options statement):
Options -Multiviews
In order to do the redirecting to the SEO friendly URLs, add this before the "Redirect external .php requests to extensionless url." rules:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /member\.php\?playername=([^&\ ]+)([^\ ]*)
RewriteRule ^ /member/%1?%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /squad\.php\?squadname=([^&\ ]+)([^\ ]*)
RewriteRule ^ /squad/%1?%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /artcile\.php\?articlename=([^&\ ]+)([^\ ]*)
RewriteRule ^ /article/%1?%2 [L,R=301]

Related

htaccess rewrite, how to negate sub directory files

I have htaccess file performing URL rewrite. It basically removes the .php extension.
The rules work fine, e.g. they successfully rewrite http://example.com/contact.php into http://example.com/contact
However the rewrite also changes sub directory files, such as /includes/check-form-input.php.
So when, for example, a form at contact.php attempts to submit to /includes/check-form-input.php the htaccess rewrite strips the .php from the end and I get 404 headers.
i.e. /includes/check-form-input is not found.
At least that's what appears to happen, using HTTP_REFERER seems to confirm this as the referer was /includes/check-form-input without the ext .php
Is there a way to add a rule to negate all sub dirs?
I have the following htaccess - which is copied from another answer here on SO, as I don't 100% grasp the rewrite structure.
(I've left in the www to non-www redirect in case it's the cause)
## Rewrite on
RewriteEngine on
## Redirect all pages from non-www to www
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
## URL rewrite
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
You can either exclude all subdirectories,
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^([^/]+)\.php$ http://example.com/$1 [R=301,L]
or limit it to only GET requests:
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ (.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/$1 [R=301,L]
or just exclude the includes subdirectory
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteCond %{REQUEST_URI} !^/includes/
RewriteRule ^([^/]+)\.php$ http://example.com/$1 [R=301,L]

.htaccess infinite loop for non existent urls

I'm working on my .htaccess to obtain an extensionless site. I've searched online and implemented some rules that are working, but when I type in a url that doesn't exist (e.g. mysite.com/dsfadfasdf ) it starts to go in an infinite loop.
If anyone would be so kind as to help me get rid of the loop and perhaps redirect to a page of my choosing (where I'll say the page doesn't exist) I'd really appreciate it!
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
# Redirect www to non www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Home page language
RewriteRule ^(en|ro)/?$ index.php?lang=$1 [L]
# For the profile page
RewriteRule ^(.+)\/accommodation-(.+)\/([0-9]+)\/(.+)$ location.php?id=$3&town=$2&hname=$4&lang=$1 [NC,L]
# Internally rewrite extensionless URL to corresponding .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
# Externally redirect (only) direct client requests for .php URLs to extensionless URLs:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://mysite.com/$1 [R=301,L]
I believe the issue is in the last rule. Any suggestions?
.htaccess keeps rewriting if the url changes. Force it to stop rewriting after you've rewritten your url internally with the END flag:
# Internally rewrite extensionless URL to corresponding .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,END,QSA]
Documentation is here.

redirecting files with html extension to files without extension (in url)

I have recently changed my website url using htaccess so that my urls will not show file extensions. Now my problem is as I have created a new xml sitemap so that my url will be extensionless!!! the Google webmaster tool is telling me about duplicate content issue!! ie. page and page.html have same title.... so my question is how do i redirect the urls with file extension html to urls with out extension!!!
this is an example of my website url with html extension
http://www.shenazhpeyk.co.uk/coding-machines.html
I want to redirect and change it to
http://www.shenazhpeyk.co.uk/coding-machines
so that will fix the issue with Google webmaster tools (Please provide me a code for use in htaccess file)
Many Thanks
Found this code as well. Not sure if it will accomplish the same thing. Seems to work for me as does the one above (for PHP).
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
I am wondering about the trailing slashes and whether those should be there or omitted?
Try adding the following to the .htaccess file in the root directory of your site redirect URLs with .html extension and remove it.
RewriteEngine on
RewriteBase /
#redirect to remove the .html extension
RewriteRule ^(.+)\.html$ $1 [L,NC,R=301]
Try this:
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

Extensionless URL trailing slash redirect

I tested the code below on several Dreamhost domains, and it works, except on newer domains added circa 2012
RewriteEngine on
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
example that works
ryanve.com/resources/lame/ and ryanve.com/resources/lame.php both redirect to ryanve.com/resources/lame and the actual file that is served is lame.php
broken example
airve.com/test/file properly serves file.php but the redirects airve.com/test/file/ and airve.com/test/file.php don't work. Try them and you'll see they seem to be spitting out the internal absolute path. The same thing occurred when I tried each of the redirects independently and when I tried the basic redirect below.
Redirect 301 /test/file/ /test/file
There is nothing else in .htaccess. Any idea on what the issue is?
You are redirecting to $1 in both cases, and you don't have a RewriteBase directive to tell Apache where to root such a relative path.
Try adding a root slash before the $1 to anchor it to the root of your webserver, for instance:
RewriteRule ^(.*)$ /$1.php [L]

How to redirect all my urls with no extension to end with .php

I originally wanted to have all my urls end with no extension. Unfortunately, I've tried many htaccess codes and I've just about given up.
So now I want to make it so if a person wants to visit a page in my site, but forgets to enter .php, he/she will automatically be redirected to the same url but with the .php
How can this be done? Thanks!
Here are the rules:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
It will check if requested resource is not a existing folder. For example: you requesting http://www.example.com/help. If there is such folder present (/help) the rule will do nothing (priority is given to a folder). If you do not want this behaviour then remove the first line.
It will check if there is such .php file before rewriting. For example: you requesting http://www.example.com/aboutus but there is NO aboutus.php file there -- no rewrite will occur.
All such requests should be without trailing slash: should be http://www.example.com/aboutus and NOT http://www.example.com/aboutus/
The rule will work for URL in subfolders as well: e.g. http://www.example.com/pages/help/aboutus will be rewritten just fine.
Because of the above checks the rule will not enter into a rewrite loop (no 500 error on this rule)
Query string (page parameters) will be preserved
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
In response to Sun Love, the code you posted works well except for situations where you have a trailing slash without the file extension (I get a 500 error) because the first RegEx doesn't match for this situation.
example.com/test.html -- works (redirects to /test)
example.com/test -- works (no redirect)
example.com/test.html -- works (redirects to /test)
example.com/test/ -- doesn't work (500 error)
There is probably a better way to do this but I added another rewrite condition to fix this:
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).php
RewriteRule ^ %1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)/\s
RewriteRule ^ %1 [R=301,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Try this .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ $1.php [L]
EDIT: ^([^\.]+)$ $1.php [L]

Resources