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]
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]
I am trying to remove the index.php from the url in codeigniter. Rewrite mod scripts are all over the place but I can't find the .htaccess file!! I tried looking in the root directory and everywhere else, but no luck.
From what I read it should be in application folder and when I go there i find the .htaccess file and all it has is deny from all. This is not the same content every one else is sharing online before modification.
Please advise.
Actually you don't find it; you create it along side with your index.php file with this contents:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php,
images, and robots.txt is treated as a request for your index.php
file.
Reference.
The solution is as follows ( for future references)
Create the .htaccess file in the root of the codeigniter application (where you got system, application, etc folders).
Paste this code in it:
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Open config.php and change index = "index.php" to "". Things should work now.
Basically I'm trying to convert my query string urls to a directory structure for SEO purposes. And I have been getting to the point where im trying to say the same logic in many semantical ways in the .htaccess file to see if I can get a result. Here is what I'm looking to do.
The request is:
mywebsite.com/test/antitrust/somelongfilename/
Then modified to request:
mywebsite.com/test/antitrust/?id=news&post=news/somelongfilename.html
So that when people click on my SEO friendly links they get changed to the ugly ones to which my php code can read. "somelongfilename.html" is not to be shown by itself, its just content thats pushed to a content area of a php file.
FYI, post=news/somelongfilename.html is actually the directory/file to goto from the antitrust directory, so its located /test/antitrust/news. Its been working fine even though its probably not a good idea to put / in a query string.
The .htaccess is in the subdirectory /antitrust, as I don't want to touch the root directory .htaccess as its servicing many websites that I dont want to affect since im not a .htaccess expert.
RewriteOptions inherit
RewriteEngine On
RewriteCond %{HTTP_HOST} news
RewriteRule ^/(.*) index.php/?id=news&post=$1 [QSA,L]
Also tried many variations of this:
RewriteEngine On
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^news$
RewriteRule \/(.+?)\/? ^?id=news&post=$2 [QSA,L]
RewriteRule ?id=(.+?)&post=(.+?) http://mywebsite.com/test/crush/antitrust/$1/$2/ [QSA,R]
Any ideas on what part im doing wrong? Thanks.
Try:
RewriteEngine On
RewriteOptions inherit
RewriteBase /test/antitrust/
# not sure why you need this, it's not part of your question
RewriteCond %{HTTP_HOST} news
# conditions to prevent looping
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ index.php?id=news&post=$1 [QSA,L]
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]
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]