I currently have urls that look like: something.com/index.php?page=pagename
we would like to have it just be something.com/pagename
But still be able to access sub folders like something.com/admin/
Thanks in advance.
If you're using Apache, which I presume you are, you need to use the ReWrite Engine:
If you don't have one already, create or add to the .htaccess file stored in the root directory you're rewriting. So you if you want something.com/index.php?* to rewrite, then use put it in the folder where something.com is stored.
There, you need something like:
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?topic=$1 [QSA,L]
This regex takes the beginning of the input after "/", and uses that input as the variable $1.
Source: http://www.generateit.net/mod-rewrite/
You then change all your links to point to "/pagename"
You may also have to turn on the RewriteEngine module by uncommenting it in your httpd.conf file by finding the line like:
#LoadModule rewrite_module libexec/apache2/mod_rewrite.so
and deleting the leading #
More info: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Related
i want a change the .htaccess file to hide the url parameters .
I would like to accept urls like
http://www.demo.com/74874 , that means http://www.demo.com?code=74874
You didn't mention what operating system you are using and instruction can vary, plus you didn't say if your are using HTTPD and if mod_rewrite is already installed/enabled. Assuming mod_rewrite is enabled and AllowOverride FileInfo is set in the server config, you could use this in your .htaccess:
RewriteEngine on
RewriteRule ^/([0-9]+) ?code=$1
This will accept any number as the path and rewrite it with the query string. If you want to keep any existing query string parameters, you would need to add [QSA] to the end of the RewriteRule.
i have a link look like this
http://backlinks.cheapratedomain.com/index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7
want to replace .index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7 this part by /backLinks_free_Backlinks/1 using .htaccess
after replacing link will be look like this
http://backlinks.cheapratedomain.com/backLinks_free_Backlinks/1
Try adding this to your htaccess file in the document root:
RewriteEngine On
RewriteRule ^backLinks_free_Backlinks/([0-9]+)$ /index.php?backLinks_free_Backlinks=$1&totalRows_free_Backlinks=7 [L,QSA]
You can remove the &totalRows_free_Backlinks=7 bit from the target if you don't need it.
I want to create a bunch of files without an extension showing at the end. The easiest way to do that was to do this:
/usa/index.php
/usa/alaska/index.php
/usa/alabama/index.php
/usa/california/index.php
What I want to do is this
/usa/alaska.php
/usa/alabama.php
/usa/california.php
and have it show up as:
/usa/alaska
/usa/alabama
/usa/california
However, I have one more level I want to add to this, the cities
/usa/alaska/adak.php
/usa/alaska/anchorage.php
/usa/california/los-angles.php
I don't want the ".php" showing up, but then each state exists as both a file and a directory. What I want is an htaccess rule that serves up the file version of the file, not the directory which is the default. I also want to strip the .php off of the end of the files so the final result looks like
/usa
/usa/alaska (alaska.php)
/usa/alaska/adak (adak.php)
I know I can get close to this by creating all the directories and using index.php for each directory, but then I will have thousands of directories each with one file in it and updating is a pain in the butt. I would much rather have one directory with 1000 files in it, than 1000 directories with 1 file in it.
Please, can someone point me in the right direction and know that I am doing this for all 50 states.
Jim
I would also suggest using a single php (e.g. index.php) file and redirecting all urls starting with usa to it, instead of separating them in different directories and files. The you'd need a couple of rewrite rules like the following
RewriteEngine On
RewriteRule ^usa/([^/.]+)$ index.php?state=$1 [L]
RewriteRule ^usa/([^/]+)/([^/.]+)$ index.php?state=$1&city=$2 [L]
So then in your index.php you'd only need to check the $_GET parameters.
Update:
If you don't feel comfortable enough to use a database and pull the needed data from there you could always use the parameters to dynamically include/require the needed files. Something like this
<?php
$source = ''; //or the 'ROOT' directory
if(isset($_GET['state'])) $source .= $_GET['state'].'/';
if(isset($_GET['city'])) $source .= $_GET['city'].'.php';
include($source); // here $source would be something like 'alaska/adak.php'
// and is assumed that the dir 'alaska' is on the same
// level as 'index.php'
?>
But to answer your original question nevertheless you could use the following .htaccess
RewriteEngine On
RewriteRule ^usa/([^/.]+)$ usa/$1.php [L]
RewriteRule ^usa/([^/]+)/([^/.]+)$ usa/$1/$2.php [L]
what about creating just one single file:
/usa/index.php
With
$_SERVER["REQUEST_URI"]
you can read the current URI.
Well, now if a user enters "http://domain.foo/usa/alaska" for example, he will get an 404 error of course.
But to call your index.php instead, you could write this line to the .htaccess:
ErrorDocument 404 /usa/index.php
Now the index.php receives everything what is written to the URI and you can match the result and include files or handle errors.
But maybe there is a better solution with .htaccess only, don't know. :)
I am using direct paths for downloading files from my site. the link is something like this
http://www.site.com/download.php?dir1/dir/dir3/file.doc
i want to wrap it with mod rewite rules so that only below link should be appeared
http://www.site.com/download
file, dir and dir3 are variable.
what i'hv to do in my .htaccess file?? Any Idea??
A simple redirect would be:
RewriteRule ^http://www.site.com/download/(.*)/?$ http://www.site.com/download.php?dir1/dir/dir3/$1 [NC,L]
This will take any request for something in the 'artificial' download directory and route it to the real location.
You can add more complex rules stripping out filetypes etc depending on your needs, or redirecting a 'name' to a filename etc etc..
e.g:
RewriteRule ^http://www.site.com/download/pdf/(.*)/?$ http://www.site.com/download.php?dir1/dir/dir3/$1.pdf [L,NC]
This would have an artificial PDF folder containing a filename ex the extension, routing to a .pdf doc....you can shape the redirect any way you like really...depends on the format you prefer
Not specific question. What is dir1/dir/dir3/file.doc means? If you want to get http://www.site.com/download.php?dir1/dir/dir3/file.doc, when you go to http://www.site.com/download do the next things in your .htaccess file.
RewriteEngine on
RewriteRule ^download/(.*)/?$ download.php?dir1/dir/dir3/file.doc [L]
I have the following URLs:
www.mydomain.com/client
www.mydomain.com/client/index.php
www.mydomain.com/client/index.php?a=b
www.mydomain.com/client/index.php?a=b&b=c
The following two htaccess files exist:
www.mydomain.com/.htaccess
www.mydomain.com/client/.htaccess
I want to edit "www.mydomain.com/client/.htaccess" so that if you go to www.mydomain.com/client, that it redirects the user to mydomain.com/client/clientarea.php. In other words, 1 and 2 must redirect to mydomain.com/client/clientarea.php, but 3 and 4 must not.
How do I do that?
Try this:
RewriteEngine on
RewriteBase /
RewriteRule ^client$ /clientarea.php [L]
RewriteRule ^client/index.php$ /clientarea.php [L]
htaccess files are parsed in the order they're discovered, so the top level one (mydomain.com/.htaccess) will be parsed and executed before Apache even considers looking down in the .../client sub-directory. As such, you'd have to modify your rewrite rule to check if the request contains a subdirectory, and NOT process it if one's found.