How to make htacces pattern - .htaccess

I would like to implement a pattern in htacces to change the url to a specific pattern.
My main file is dashboard.php. One GET variable is constant and another are optional.
Here is an example illustrating my idea:
dashboard.php?view=foo&var1=bar&var2=something
Redirects to
dashboard/foo?var1=bar&var2=something
I'm not good at writing conditions to .htacces.
Thank you for any help you can give.

You can use this rule in your site root .htaccess:
Optiona -MultiViews
RewriteEngine On
RewriteRule ^(dashboard)/([\w-]+)/?$ $1.php?view=$2 [L,QSA,NC]
References:
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Apache mod_rewrite In-Depth Details

Related

.htaccess rewrite rule mod_rewrite

I have a URL:
http://localhost/sample/custom blog/controllers/sample.php
and I am trying to rewrite it to look like:
http://localhost/sample/custom blog/sample/
I have tried the following but it doesn't seem to work out.
RewriteEngine On
RewriteRule ^custom Blog/([0-9a-zA-Z]+)/(.*) $2 [L]
This to my knowledge should get me at least like this:
http://localhost/sample/custom blog/sample.php
Please help me with this.
And also if you could let me know where I can learn the basics of htaccess, I 'll be delighted.
Thanks.
Place this code in /sample/.htaccess:
RewriteEngine On
RewriteBase /sample/
RewriteRule "^(custom Blog)/([0-9a-zA-Z]+)/$" $1/controllers/$2.php [L,NC]
Make sure there is no .htaccess in custom Blog/ folder.
Reference: Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details

301 Redirection of url Pattern in htaccess

Previously I have vbulletin forum installed on main domain. Now I have replaced it with WP blog and shifted forum to subfolder. Both WP and vB has seperate htaccess files. Please help me to redirect old forum urls to new ones.
Old url pattern:
www.domain.com/f1/post-title/
www.domain.com/f2/post-title/
www.domain.com/f3/post-title/
New url pattern:
www.domain.com/forums/f1/post-title/
www.domain.com/forums/f2/post-title/
www.domain.com/forums/f3/post-title/
Please somebody help me with rewriting rules for correct redirection. Also mention which htaccess (WP or vB) to put the code. Thanks in advance.
It needs to be placed on the .htaccess on the root folder of your domain.
So if your root folder is /home/youraccount/public_html/ then in the .htaccess in that folder.
This will redirect as you asked above, any forum/topic to forums/forum/topic
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^(f\d+)/([^/]+)/?$ /forums/$1/$2/ [R=301,L]
I could use ([^/]+) twice but since you mentioned you have a WordPress in the root now then you should need a more specific rule for the first folder like the above.
This will match the forum id aka f1, f2 ... up to any amount of numbers:
(f\d+)
This will get anything not a / so it will get the post id and title altogether.
([^/]+)
If you have more rules inside your .htaccess file make sure you place this rule after RewriteEngine on and before any other rule, so it doesn't conflict with other rules and redirect as you asked:
RewriteRule ^(f\d+)/([^/]+)/?$ /forums/$1/$2/ [R=301,L]

Custom .htaccess rules for web app

Ok, I'm trying to undersand mod_rewrite but I end up always confused with all this, so I'm asking if someone can help me with this custom rules.
My web app will use 3 kinds of pages which currently have these URLs:
url.com/index.php?user=name is for the user profiles
url.com/index.php?page=name is for the front-end pages (about, contact, etc...)
url.com/index.php?dash=name is for logged-in area pages
But I want the first 2 to be url.com/name and the 3rd to be url.com/dashboard/name.
I guess that for the user and page I'll have first to check if page exists, then check usernames and if none of this exists, then return 404.
How can I do this?
Thanks in advance :)
These type of custom rules are defined using Regex.
Check the kind of page using RewriteCond and define RewriteRule accordingly.
Complete information is available here
You'll be better off having 3 URL schemes:
url.com/user/name
url.com/page/name
url.com/dashboard/name
If that is acceptable then you need to enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /webapp/
RewriteRule ^user/(.+?)/?$ index.php?user=$1 [L,QSA,NC]
RewriteRule ^page/(.+?)/?$ index.php?PAGE=$1 [L,QSA,NC]
RewriteRule ^dashboard/(.+?)/?$ index.php?dash=$1 [L,QSA,NC]

Using .htaccess to rewrite clean & custom URL structure

How do i redirect
website.com/about.php
to
website.com/about
Also, is it possible to manually create the appearance of subdirectories using .htaccess?
i.e. website.com/project1.php
to
website.com/projects/project1
Much appreciated!
Yes!
You can use the Apache RewriteEngine module to achieve this end. The documentation is here: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
There are a number of good tutorials around also.
Some examples of rules are:
RewriteRule ^/(\S+).php /$1
RewriteRule ^/(project\d+).php /projects/$1
However, these are just guesses at what you might want to achieve, there are always corner cases. (Also, these are not tested!)
Bear in mind the above two rules would not necessarily be good together as written. Take note in the documentation about setting up the RewriteEngine and using appropriate RewriteRule options.
For example, when specifying many RewriteRules it is common to specify the [L] option so that the RewriteEngine stops rewriting after the rule is applied. Ordering of rules, therefore, can be significant.
Here are your examples putting the .htaccess on your root directory and using mod_rewrite
RewriteEngine on
RewriteRule ^about\.php$ about [R]
RewriteRule ^projects/project([0-9]+)$ project$1.php

PHP Rewrite Rules

The actually URL which my app uses is:
http://site.com/search.php?search=iPhone
but I would like it to be possible to achieve the same with
http://site.com/iPhone
I have no experience of rewrite rules, how can I set this up?
The solution has worked but the new URL is displayed in the address bar. I thought it would have been possible to set this up so that it appears as though the page location is
http://site.com/iPhone
without changing to display
http://site.com/search.php?search=iPhone
Is this possible? Thanks.
Create a file called .htaccess in the root of your website and put this in it.
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*) search.php?search=$1 [R]
Should do the trick.
I would suggest however that you make it a bit more specific, so maybe require the user of a search directory in your url. eg instead of mysite.com/IPhone use mysite.com/search/IPhone which would work like
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^search/(.*) search.php?search=$1 [R]
This makes it easier to have normal pages that arnt redirected, such as about us or a basic homepage.
As Chris says, this is not PHP but Apache that does this, and whether it works can depend on your hosting setup.
You need to specify something like this in your .htaccess file:
RewriteEngine on
RewriteRule /(.*) /search.php?search=$1
Check also:
mod_rewrite: A Beginner's Guide to URL Rewriting
Module mod_rewrite, URL Rewriting Engine
Rewrite rules aren't part of PHP as far as I'm aware, but Apache (specifically mod_rewrite) or whatever server you're using. For Apache, you need on the server to have a file called .htaccess, and in it put something like:
RewriteEngine on
RewriteRule ^(\w+)/?$ /index.php?search=$1
^(\w+)/?$ is a regular expression - it matches any word of 1 or more characters, followed by a / maybe. So it changes site.com/iPhone into site.com/index.php?search=iPhone. Sound about right?

Resources