htaccess to remove .php .html extension if exists from URL - regex

I have pages like the following
http://www.domain.com/p.php?p=InfoPages&file=about.php
http://www.domain.com/p.php?p=InfoPages&file=contact
http://www.domain.com/p.php?p=InfoPages&file=post.html
I want to change them to
http://www.domain.com/page/about
http://www.domain.com/page/contact
http://www.domain.com/page/post
RewriteRule ^page/([^/]*)$ /p.php?p=InfoPages&file=$1
I am currently using the above code in .htaccess but it doesn't work if there is a .php or .html extension in the url.
I've googled for this but still i can't find a perfect way to solve since not all links have extensions.
please advise. thanks.

Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /p\.php\?p=([^&]+)&file=([^.]+)\.php|html [NC]
RewriteRule ^ /page/%2? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]+)/?$ /p.php?p=InfoPages&file=$2 [NC,QSA,L]

Related

htaccess removing .php extension and pretty urls

Im trying to both remove .php extensions. So for example "http://localhost/timetable/login" instead of "http://localhost/timetable/login.php"
But also have
"http://localhost/timetable/38/" instead of
"http://localhost/timetable/index.php?week=38"
Im able to get one or the other working but not both at the same time. Im assuming its because there is a conflict between them but Im not advanced enough to find it.
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([0-9]+)$ index.php?week=$2
RewriteRule ^([0-9]+)/$ index.php?week=$2
If in the address bar I type "http://localhost/timetable/38" it brings me to "http://localhost/38/" and an Object not Found error.
Does anyone know what the problem is ?
UPDATE: I can now go to the page but
echo $_GET['week'];
Is returning empty result, so its ignroing the 40 in "http://localhost/timetable/40"
Instead of using separate rewrite rule for each input, you should consider routing all of them as a single string to some php file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [L,QSA]
In you php file, you can then separate the string as input and use them as required.
<?php
$inputs = explode('/', $_GET['page']);
You only have one capture group when you try to get the week. So it should be $1 instead of $2.
According to this test tool, the following should work:
RewriteRule ([0-9]+)/?$ index.php?week=$1
I would do something like this:
# rewrite if url ends with a number and possibly a slash
RewriteRule ([0-9]+)/?$ index.php?week=$1 [QSA,L]
# do not append .php if it already ends with .php, other add .php
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [QSA,L]

Htaccess rewrite URL with virtual directory and 2 variables

its my URL...
result.php?team=arsenal&player=ospina
I want to like this
mysite.com/virtualdirectory/arsenal/ospina.html
I tried this code.. cant work.. Not Found
RewriteRule ^(.*)/(.*)/(.*).html$ result.php?team=$2&player=$3 [L]
The requested URL /subfolder/arsenal/ospina.html was not found on this server.
Apache/2.2.8 (Win32) PHP/5.2.6 Server at localhost Port 80
Thanks for helps,
Best regards!!
ok , I assume you want to change the URI from
http://www.example.com/result.php?team=arsenal&player=ospina
to
http://www.example/subdirectory/arsenal/ospina.html
so this is the .htaccess that will do that for you
RewriteEngine on
RewriteCond %{QUERY_STRING} ^team=(.*)\&player=(.*)$
RewriteRule ^(.*)$ http://www.example.com/subdirectory/%1/%2.html [R=301]
you can test it with htaccess tester here
http://htaccess.madewithlove.be/
and some useful links for documentation and learning:
http://httpd.apache.org/docs/2.2/rewrite/intro.html
http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708
Great Video Tutorials - Understanding Apache and htaccess
hope that helps
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^\.*]+)/([^\.*]+)/([^\.*]+).html$ result.php?team=$2&player=$3 [L,QSA]

URL Rewriting and htaccess files

Hoping someone can help. I use nibbler to check out my website to make sure it behaves well and one of the categories is URL FORMAT which
Avoid use of file extensions wherever possible.... Consider URL rewriting as an effective
and transparent means of creating appropriate URLs.
Anyway, I use Dreamweaver to create and edit the site and if I convert it to using index.htm files in directories (which would solve this) then this just seems to make life complicated.
Is there some MAGIC that I can do in the .htaccess file that means i can still upload it as .htm's and it does some cool stuff in the background and shows them better.
for example I would like
http://www.beingchildren.org/Children-Charity-Blog.htm
to become
http://www.beingchildren.org/Children-Charity-Blog
Can anyone help?
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.htm[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.htm -f
RewriteRule ^(.+?)/?$ /$1.htm [L]

301 redirect www.root.com/subfolder to www.subfolder.com on same server

Hi people at Stackoverflow,
I've honestly searched here and tried stuff but since I'm really not an expert and nothing worked, I'm lost and need your help. I don't post here often because usually I find what I am looking for.
The situation
I have different sites running on the same server. Let's say that one of my websites is called Cats. It runs with CMSMS and resides in a subfolder of my root called cats.com.
In the root of my server there is a htaccess file with this code (generated by the server admin):
RewriteCond %{HTTP_HOST} ^www.cats.com$
RewriteCond %{REQUEST_URI} !^/cats.com/
RewriteRule (.*) /cats.com/$1
RewriteCond %{HTTP_HOST} ^cats.com$
RewriteCond %{REQUEST_URI} !^/cats.com/
RewriteRule (.*) /cats.com/$1
In the cats.com subfolder there is a htaccess file with rewrite code for pretty urls:
# RewriteBase /cats.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
This works. When I request www.cats.com I see the contents of the /cats.com subfolder in my root but the URL base stays www.cats.com and all the URLS are extremely pretty.
However, due to a setup error (my bad) in my site's config file, the URL listed in Google is
http://www.root.com/cats.com
When I click the URL, I see the correct page but the address bar reads http://www.root.com/cats.com. The pretty URL rewrite works, but I want this url to rewrite to www.cats.com.
What I have tried
I have tried rewriting the Google listed URL with
attempt one
I tried this in both htaccess files, below and above the existing rewrite rules.
RewriteRule ^/cats.com(.*)$ http://www.cats.com [R=301,L]
RewriteRule ^/cats.com/(.*)$ http://www.cats.com/$1 [R=301,L]
Nothing happens. Everything stays the same.
attempt two
Again tried this in both htaccess files, below and above existing rewrite rules
Redirect 301 /cats.com http://cats.com
This results in an infinite loop in all occasions.
I hope my description is clear enough...
If anybody has any idea what might / should work I'd love to hear it.
Thank you for your help!
Change your rules in cats.com subfolder's htaccess file to this:
RewriteEngine On
RewriteBase /cats.com/
RewriteCond %{THE_REQUEST} /+(cats\.com)/(\S+) [NC]
RewriteRule ^ http://%1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]

regex in my RewriteRule is OK, but .htaccess does not work

I'm having a lot of trouble getting my .htaccess ReWrite to work on my apache web server. I've read several tutorials and tested my regex matching with Grep.
Here is the code:
RewriteRule \?action=viewArticle&articleId=([0-9]*)&categoryId=([0-9])$ essays/$1 [R=301,L]
here is a url I'm trying to match:
http://mysite.com/?action=viewArticle&articleId=15&categoryId=1
and change to
http://mysite.com/essays/15
UPDATE: Solution! with a very excellent tutorial from Jon. It was very important that I put <base href="/"> in my header file to get the css to work correctly.
Final rewrite looked like this:
RewriteCond %{THE_REQUEST} /?action=viewArticle&articleId=([0-9]*)&categoryId=([0-9])
RewriteRule ^$ /essays/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^essays/([0-9]+) /?action=viewArticle&articleId=$1&categoryId=([0-9]) [L]
You can't match hosts or query strings inside a RewriteRule, you need to match against the %{HTTP_HOST} and %{QUERY_STRING} variables in a RewriteCond directive:
RewriteCond %{HTTP_HOST} mysite\.com$ [NC]
RewriteCond %{QUERY_STRING} ^action=viewArticle&articleId=([0-9]*)&categoryId=([0-9])$
RewriteRule ^$ /essays/%1? [L,R=301]
This redirects the browser (changing the URL in the address bar) when someone goes to http://mysite.com/?action=viewArticle&articleId=15&categoryId=1 to http://mysite.com/essays/15
You don't need the %{HTTP_HOST} condition if your htaccess file only serves a single host.