Using .htaccess I am trying to add a directory to all URLs but don't seem to be having any luck. I can do it for each directory but was wondering if there is an easier way around this.
I need all my urls such as:
www.example.com/abc
www.example.com/def
www.example.com/ghi
to append the directory 'test' making the URLs look like this:
www.example.com/test/abc
www.example.com/test/def
www.example.com/test/ghi
Tried with PHP but that seemed kind of silly so .htaccess seemed like the best bet.
Using mod_rewrite you can try adding this to your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /test/$1 [L]
If you absolutely need a redirect (as opposed to a "behind-the-scenes" rewrite), change the [L] to [R,L]
Related
The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]
So I'm building an ecommerce website using a file structure suggested to me, it looks like this:
http://www.sample.com/index.php
http://www.sample.com/department/index.php
http://www.sample.com/department/men/index.php
Basically I want to get rid of all the index.php filenames in the url, so it looks like this:
http://www.sample.com
http://www.sample.com/department
http://www.sample.com/department/men
I've achieved this for my homepage with adding this code in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
But, is there any way to apply this rule to every subdirectory in my site with just editing the root .htaccess file? I'm guessing it can be done by adding this rule in .htaccess files in every directory but I'd rather just have one.
Also, is this even a good way to structure a website? I am not particularly fond of it as there are so many index.php files but I'm not sure of a better alternative...
Try putting these rules above the rules that you have in your htaccess file in the document root:
RewriteRule ^index.php$ / [L,R=301]
RewriteRule ^(.+)/index.php$ /$1/ [L,R=301]
I am working on one website.
On this website, porfile URL currently is like this:
http://eprofile.co/eprofile.php?user=degroundshaker
I want to rewrite this URL as:
http://eprofile.co/degroundshaker
This, is addon domain so its files are under a folder called "eprofile.co" in my cPanel and there is one .htaccess file.
So, i need solution and please let me know what rule i need to add and what should be the complete format in .htaccess
I m newbie in .htaccess.
Try this:
RewriteEngine On
RewriteRule ^([^/]*)$ /eprofile.php?user=$1 [L]
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /eprofile.php?user=$1 [L]
Then you need to change all of your profile links from looking like this:
http://eprofile.co/eprofile.php?user=degroundshaker
to looking like this:
http://eprofile.co/degroundshaker
EDIT:
Alternatively, you could do:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/eprofile.php
RewriteRule ^([^/]*)$ /eprofile.php?user=$1 [L]
I have created a simple mod rewrite for my site:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(cgi-bin|css|images|js|gfx)/?.*$ [NC]
RewriteRule ^filter/model/([^/]*)/mins/([^/]*)/texts/([^/]*)/freegifts/([^/]*)/network/([^/]*)/merchant/([^/]*)$ /fiverrr.php?model=$1&mins=$2&texts=$3&freegifts=$4&network=$5&merchant=$6 [L]
but it doesn't seem to be working (as in all the images/js etc don't show up)
e.g. http://mydomain.com/filter/model/Sony+Ericsson+Xperia+Arc+S+White/mins/+3000/texts/+-1/freegifts/FREE+Nintendo+3DS+Black/network/Orange/merchant/
original url: http://mydomain.com/fiverrr.php?model=Sony+Ericsson+Xperia+Arc+S+White&mins=+3000&texts=+-1&freegifts=FREE+Nintendo+3DS+Black&network=Orange&merchant=Contract
Where am I going wrong, and is this the best method?
Thanks so much
Try to put there another condition - htaccess will rewrite it only if requested file does not exist:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^filter/model/([^/]*)/mins/([^/]*)/texts/([^/]*)/freegifts/([^/]*)/network/([^/]*)/merchant/([^/]*)$ /fiverrr.php?model=$1&mins=$2&texts=$3&freegifts=$4&network=$5&merchant=$6 [L]
Also I recomend you to check full paths. Check if you have images in HTML code like: http://mydomain.com/images/img.jpg - relative paths like ./../images/img.jpg will not work!
I want to make
http://domain.com/index.php?query=query
look like
http://domain.com/query
I know I need to use .htaccess, but I have no idea how to approach this.
Something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteBase /
RewriteRule ^(.+) /index.php?query=$1
Edited:
If you really want your URLs to look like you asked, you should host all your media files (CSS, JS and images) in another virtual host, lets say, http://media.domain.com - because can't tell the difference if "query" matches the name of an existing file on domain.com.
The keyword to search for is RewriteRule.
The Drupal .htaccess file is a good example of mapping /?q=query to /query, but not redirecting things which provide an explicit match - so /files/something.css which is a real file will not be redirected. Here's the relevant snippet from Drupal's .htaccess with ?q= changed to ?query=.
RewriteEngine On
RewriteBase /
# Rewrite URLs of the form 'index.php?query=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?query=$1 [L,QSA]