I am very new to apache and mod re-write I am trying to make pretty links using .htaccess. My URL looks like this:
http://example.com/single_picture.php?name=Testing-123&cat_id=1
I want to make it look like
http://example.com/pictures/Testing-123/
That is just the name. Here is what I have put in my .htaccess file but it is not working.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^pictures/(\s+)*$ ./single_picture.php?name=$1&cat_id=$2
How do I do it?
Ahmar
Try This:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^pictures/([A-Za-z0-9\-_]+)/([A-Za-z0-9\-_]+)/?$ /single_picture.php?name=$1&cat_id=$2
Also remember that a rewrite will not rewrite the request a user has made. i.e. http://example.com/single_picture.php?name=Testing-123&cat_id=1 will work as will http://example.com/pictures/Testing-123/1/.
You will need to make the user view the page via the SEO link by changing the links on your website to reflect this.
Related
I started learning basics of .htaccess file and here i wanted to know how can i create tree stucture url in php using .htaccess file. For example, I have hierarchy like :- science->biology->zoology->dermatology. and domain is www.mydomain.com. So
If I click on science, Url should be www.mydomain.com/science.
Under science If I click on biology, Url should be www.mydomain.com/science/biology.
Under biology If I click on zoology, Url should be www.mydomain.com/science/biology/zoology. and so on.
Here is what I tried so far, created a page called as branch.php, If i click on science i am writing code in branch.php file by using url www.mydomain.com/branch/science and htacces file
Options -Multiviews
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^branch/(.*) branch.php?branch_name=$1 [L]
So I can i create url like www.mydomain.com/science/biology/zoology without creating page(branch.php) using .htacces file.
There is a little disorientation in your thoughts.
Any kind of url you are generating in the PHP without any restrictions.
And .htaccess (and mod_rewrite in particular) used only for redirection of request (transparent for users) to the particular php-file.
So, yes, you can do url like this: http://www.example.com/science/biology/zoology and your .htacces should be like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* branch.php [L]
Then, in the branch.php you should examine $_SERVER['REQUEST_URI'] (it will look like this /science/biology/zoology) and make a decision what to show to the user.
RewriteCond %{REQUEST_FILENAME} !-f is needed to test that requested uri didn't point to real file.
UPDATE: If you want to use $_GET array in the branch.php you can do the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ branch.php?main_branch=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ branch.php?main_branch=$1&sub_branch=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ branch.php?main_branch=$1&sub_branch=$2&sub2_branch=$3 [L]
I've created an htaccess file for my website that converts a URL like this http://www.example.com/fixture?id=1 to something like http://www.example.com/fixtures/1.
My htaccess file:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^fixtures/(\d+)*$ fixture.php?id=$1
That works how I want it, but isn't exactly SEO friendly, so I'd like to pass in two more variables home and away like so http://www.example.com/fixture?id=1&home=Arsenal&away=Chelsea.
Then rewrite the URL to look like `http://www.example.com/fixtures/arsenal-vs-chelsea.
There are many questions about rewrite rules on SO, but none discuss adding a small portion of text between variables. How could I make it so that I could add the vs portion of the URL using a rewrite rule?
You can use:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^fixtures/([^/]+)-vs-([^/]+)/?$ fixture.php?home=$1&away=$2
Rewrite to:
http://www.example.com/fixture?home=Arsenal&away=Chelsea
I am working on urls of a site. I want to convert the following url into
http://dev.steelogic.com/solution.php?id=metalbuildingproduct
this url
http://dev.steelogic.com/solution/metalbuildingproduct
this is the htaccess code I am using.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/(.+)$ $1.php?id=$2
I don't know anything about htaccess. usually I just copy paste the text and it works.
the url with ?id= works fine
but when try to use the 2nd url it doesn't have any id.
I have to do this to many other pages as well and the all contain [.php?id=] which I want to replace with [/] slash
Try this rule in your DocumentRoot/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1.php?id=$2 [L,QSA]
Please try this:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteBase /
RewriteRule ^solution/([^/.]+)/?$ solution.php?id=$1 [PT,L,NC,QSA]
</IfModule>
Because you say that even without this rule, going to http://example.com/solution/asdf loads /solution.php on your server, something else must be working on your url. Because you say you didn't define such a rule, it must be coming from somewhere else. Either a rule is defined somewhere else (main config file) or MultiViews is enabled. If you can't apply anubhava's solution (disabling MultiViews), move your file somewhere else, for example under /public/solution.php, so that url never partly matches an existing file, then use the following rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/$1.php -f #partly copied from anubhava's answer
RewriteRule ^([^/]+)/([^/]+)/?$ /public/$1.php?id=$2 [L]
from all of the discussion I concluded that its difficult enough to do it with .htaccess as there might b some sort of module working on the urls of site from the hosting and I get both solution and solution.php directed to same page
so I did it in php for now.
as both of the following pages
http://dev.steelogic.com/solution.php?id=metalbuildingproduct
and
http://dev.steelogic.com/solution/metalbuildingproduct
being going to same pages but with the second url I was not getting query string var. so I wrote a PHP code as I knew on each page how many vars I need so it worked for me. although it may not be a good approach but it works
$URI = explode("/", trim($_SERVER['REQUEST_URI']));
$numElems = count($URI);
if ($numElems <= 4) {
$arr = array();
$arr['id'] = $URI[2];
if (!empty($URI[3]))
$arr['id2'] = $URI[3];
}
if any body can help me with .htaccess I will appreciate.
So to begin with I have a custom url rewrite that sends a request variable to a php script
Rewrite rule is below:
RewriteRule ^([\w\/-]+)(\?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]
So if you access something like domain.com/slug-text it sends slug-text to index.php located in folder named test.
What I want is all my urls to look like domain.com/slug-text.html, but slug-test variable should still be sent to index.php file.
And
What I can't figure out is the redirect. I want all the old urls to be redirected from domain.com/slug-text or domain.com/slug-text/ to domain.com/slug-text.html and slug-text sent to index.php file located in test folder.
Searched a lot but could not find the answer for this question anywhere on the Internet.
Thank you all for the help.
UPDATE:
my new code is:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/\-]+)?[\w-])(?!:\.html)$ http://domain.com/$1\.html [L,R=301]
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
domain.com/slug-text/ does not get redirected to domain.com/slug-text.html
domain.com/slug-text works as intended redirecting to domain.com/slug-text.html
What do i need to change?
This rule:
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
Will trap domain.com/slug-text, domain.com/slug-text/ and domain.com/slug-text.html and send slug-text to /test/index.php inside slug param.
If you really want to redirect using [R=301] from old urls to new then use this:
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
Also note that as using explicit redirect bottom rule is modified to trap url's ending with .html
It is also advisable (if your .htaccess does not already contain this) to filter conditions for existing files and folders not to be trapped by your redirect rules. Simply add these lines before RewriteRule lines:
# existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# existing folder
RewriteCond %{SCRIPT_FILENAME} !-d
And if using symlinks:
# enable symlinks
Options +FollowSymLinks
# existing symlink
RewriteCond %{SCRIPT_FILENAME} !-l
// addition
Your .htaccess file should look like this:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
This is supposed to redirect /slug-text to /slug-text.html
RedirectMatch ^/([\w-]+)/?$ http://domein.com/$1.html
This is in the case when slug-text is only letters, digits, – and _.
Rewrite slug-text.html to a php file and pass the slug as a param:
RewriteRule ^([\w-]+)\.html$ test/index.php?slug=$1 [R,L]
If you have both line in your .htaccess the first one will do the redirects from the legacy URLs to the new ones and the second one will process the request.
So i have a mvc system setup but it does not generate search engine friendly urls.
A typical url is in the format:
http://sitedomain.com/class/classMethod?parameter=valueA?parameter2=valueB
This is what i need to have:
http://sitedomain.com/class/valueA/valueB/
My .htaccess actually modified a part of the url already but i dont know how to do the second part
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?controller=$1 [L,QSA]
(originally looked like http://site.com/index.php?controller=class, but after the htaccess below is ran, it looks like http://site.com/class)
If anyone could help me with this, that would be great, thank you.
RewriteRule ^/class/(.*)/(.*)/$ index.php?controller=class¶meter=$1¶meter2=$2 [L,QSA]
I use the following .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ rewrite.php?%{QUERY_STRING}
And extracting parts from the url is all done in PHP.
Parsing the $_SERVER['SCRIPT_NAME'] variable.
(I find php code much easer to debug than complex apache rewrite rules.)