How to hide page name and extention from url using .htaccess - .htaccess

I want to hide all my page names and extension from url,
htt://www.domain.com/innerpage.php
to
http://www.domain.com/
and
http://www.domain.com/subfolder/innerpage.php
to
http://www.domain.com/subfolder/
and
http://www.domain.com/subfolder/subfolder/innerpage.php
to
http://www.domain.com/subfolder/subfolder/
I used like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://www.domain.com/$1/ [R=301,L]
RewriteRule ^(.*)$ http://www.domain.com/subfolder/$1/ [R=301,L]
RewriteRule ^(.*)$ http://www.domain.com/subfolder/subfolder/$1/ [R=301,L]
RewriteRule ^(.*)/$ $1.php [L]
its not work

I think this will work for you
DirectoryIndex innerpage.php index.php index.html index.htm
Put this as first line of your .htaccess. This directive will look for innerpage.php if no page has been specified for a directory. If you want to hide all pages in your site URL's then it's not a good idea IMO.

Give this set of directives a try:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/?$ /innerpage.php
RewriteRule ^([a-z0-9]+)/?$ /$1/innerpage.php
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /$1/$2/innerpage.php
RewriteRule ^innerpage.php$ / [R]
RewriteRule ^([a-z0-9]+)/innerpage.php$ /$1 [R]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/innerpage.php$ /$1/$2 [R]
And be surprise what will happened...

Related

.htaccess redirect all index.html to parent folder

I need to redirect all index.html like this
Original URL
www.example.com/lp/index.html
www.example.com/sytem/index.html
Desired URL
www.example.com/lp
www.example.com/sytem
I used the follwing, it redirects successfully but 404 page
RewriteEngine On
RewriteRule ^index\.html$ [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
You almost got it right, what you did looks like a copy paste error to me. Your first RewriteRule is missing a parameter - there should be a slash before [R=301,L]
Your htaccess should look like this:
RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
Reference:
http://dense13.com/blog/2012/10/29/removing-index-html-with-mod_rewrite-in-htaccess/
Place this code in your DOCUMENT_ROOT/.htaccess file:
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^(.*?)index\.html$ /$1 [L,R=301,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1/index.html [L]
Try putting this in your .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1/index\.html [PT,L]
PT and L are rewrite flags, for more details about them check http://httpd.apache.org/docs/2.2/rewrite/flags.html
why not redirect index.(anything) ? like index.php? index.xhtml?
RewriteEngine On
RewriteRule ^index\.(.*)$ / [R=301,L]
RewriteRule ^(.*)/index\.(.*)$ /$1/ [R=301,L]
or to base it from the initial URL request only (to avoid conflicts with other rewrite rules):
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.(.*) [NC]
RewriteRule ^(.*?)index\.(.*)$ /$1 [R=301,L]

htaccess pretty url to regular php file

I am trying to use my htaccess to allow me to send in a pretty url and have it route to a regular webpage.
So while I am sending in "http://www.foobar.com/dir1/file/id" I would like it to route to "http"//www.foobar.com/dir1/file.php?id=1" As I mentioned, I have been trying to achieve this through my htaccess file but I am just getting 404's all the time. I was hoping one of the gurus here can help me.
This is my htaccess on the dir1 directory:
Options +FollowSymLinks
RewriteEngine on
RewriteEngine on
RewriteRule ^dir1/file/([a-zA-Z0-9]+)$ file.php?id=$1 [L]
Thanks!
Try This its working fine
//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ file.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ file.php?id=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
Or try this
Options +FollowSymLinks
RewriteEngine On
# add trailing slash
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# rewrite gallery/ to gallery.php
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]
# redirect example.com/index.php to example.com/
RewriteCond %{REQUEST_URI} index\.php$
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]
try this:
<FilesMatch "\.(tpl|ini|log)">
Order deny,allow
Deny from all
</FilesMatch>
here you can change (tpl). file into your needs
and further
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

Issue with subdomains and htaccess for SEO

I would like to change urls from:
http://subdomain.domain.com/page/ to http://subdomain.domain.com/?page=pagename
and also:
http://domain.com/page/ to http://domain.com/?page=pagename
though haven't had much success.
Here is my htaccess file so far [updated]
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Remove 'www'
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://resolutiongaming.com/$1/ [L,R=301]
# Subdomain redirect
RewriteRule ^/(webdev)/(.*)$ http://webdev.resolutiongaming.com/$1 [R=301,L]
RewriteRule ^/(artwork)/(.*)$ http://artwork.resolutiongaming.com/$1 [R=301,L]
RewriteRule ^/(music)/(.*)$ http://music.resolutiongaming.com/$1 [R=301,L]
# Search engine optimization
RewriteRule ([a-zA-Z]+)/$ ?page=$1
I used RewriteRule ^([a-zA-Z]+)/$ ?page=$1 which seemed to work for the domain url but not the subdomain. Any help would be appreciated.
Actually had to do something like this recently. Try this for your Subdomain redirect block:
RewriteRule ^/(webdev)/(.*)$ http://webdev.resolutiongaming.com/$2 [R=301,L]
RewriteRule ^/(artwork)/(.*)$ http://artwork.resolutiongaming.com/$2 [R=301,L]
RewriteRule ^/(music)/(.*)$ http://music.resolutiongaming.com/$2 [R=301,L]
Or maybe this; note the change from $2 to $1:
RewriteRule ^/(webdev)/(.*)$ http://webdev.resolutiongaming.com/$1 [R=301,L]
RewriteRule ^/(artwork)/(.*)$ http://artwork.resolutiongaming.com/$1 [R=301,L]
RewriteRule ^/(music)/(.*)$ http://music.resolutiongaming.com/$1 [R=301,L]
EDIT: Or Maybe try this. Note that you need to capture two parts of the URL to rewrite as you are explaining. Each item in parenthesis ( and ) corresponds to a string in the rewrite. Try this. Using your nicer regular expression RewriteRule as you mention in the comments:
RewriteRule ^([a-zA-Z]+)/(.*)$ /$1/$2 [R=301,L]
Or maybe this:
RewriteRule ^([a-zA-Z]+)/(.*)$ /$1/page=$2 [R=301,L]
So it was an easy fix really. I basically put a .htaccess file in each subdomain directory that looks like this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Search engine optimization
RewriteRule ([a-zA-Z]+)/$ ?page=$1
Hope this helps someone. :D

htaccess adding query string to 301 redirect

Can sombody help me out with this, im trying to re direct a page using htaccess file but it keeps adding ?c=oldpage on to the end of the new url, example:
http://www.mydomain.co.uk/newpage.html?c=oldpage
i have tried some of the solutions posted here but no luck, here is my .htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule (.*) http://www.mydomain.co.uk/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteEngine on
RewriteRule ^product/(.*).html$ product.php?p=$1 [L]
RewriteRule ^(.*)\.html$ category.php?c=$1 [L,NC]
Redirect 301 /oldpage.html http://www.mydomain.co.uk/newpage.html
ErrorDocument 404 /404.php
Thanks for any help.
This is mod_alias (the Redirect directive) and mod_rewrite not playing nicely with each other. Because both modules apply their directives on the same URI in the URL-file mapping pipeline, they don't know to ignore each other since neither directive knows what the other module is doing. Since you're targets overlap, both modules are applying their directives on the same URI and you get a mish-mashed result.
You need to stick with mod_rewrite in this case and move the redirect above the internal rewrites:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
RewriteEngine on
# redirects
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule (.*) http://www.mydomain.co.uk/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^oldpage.html$ http://www.mydomain.co.uk/newpage.html [R=301,L]
# internal rewrites
RewriteRule ^product/(.*).html$ product.php?p=$1 [L]
RewriteRule ^(.*)\.html$ category.php?c=$1 [L,NC]
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.net$
RewriteRule ^example\.html$ "http\:\/\/abc\.net\/example\/" [R=301,L]
RewriteOptions inherit
to a folder, or:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.net$
RewriteRule ^example\.html$ "http\:\/\/abc\.net\/example\.html$" [R=301,L]
RewriteOptions inherit
Don't know much about it, but it's what i use, hope it helps.

mod_rewrite config for /

I have the following .htaccess config
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule \.git - [F,L]
RewriteRule ^help help.php [L]
RewriteRule ^home home.php [L]
RewriteRule ^profile profile.php [L]
RewriteRule ^index index.php [L]
RewriteRule ^users/([0-9]+) profile.php?id=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)?$ profile.php?u=$1 [L]
Now, whenever somebody visits the landing page, they get redirected using the last rule for profile.php?u=$1.
How do I change the configuration so that www.example.com and www.example.com/ are mapped to index.php and not profile.php?
Match the empty string or single slash just after the ^index rule:
RewriteRule ^help help.php [L]
RewriteRule ^home home.php [L]
RewriteRule ^profile profile.php [L]
RewriteRule ^index index.php [L]
# Match root request with optional slash
RewriteRule ^/?$ index.php [L]
I will suggest not to do it this way.
Instead, simply user this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This will send all your requests to index.php page, from there create a router.php and pass on the requests to that page, using php.
but in case you do, just add
RewriteRule ^/?$ index.php [L]
Like Michael suggested.
Here is a simple tool to test your rules, if you wish to
Apache RewriteRule tester

Resources