how to use mod_rewrite .htaccess - .htaccess

Hello i want to rewrite the following link:
http://websoftit.ro/social/profil.php?user=Ancuta Mirela
to http://websoftit.ro/social/Ancuta Mirela.
Can someone tell me how to do that with mod_rewrite also maybe give me a link to a good documentation to learn myself how to do that?
Thnx in advance.

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+social/profil\.php\?user=([^\s&]+) [NC]
RewriteRule ^ /social/%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^social/([^/]+)/?$ /social/profil.php?user=$1 [L,QSA]
I would suggest http://askapache.com as a good reference for learning mod_rewrite.

RewriteEngine On
RewriteRule ^social/([^/]+)$ social/profil.php?user=$1

RewriteCond %{QUERY_STRING} (?:^|&)user=([^&]+) [NC]
RewriteRule ^ http://websoftit.ro/social/%1 [NE,R=301,L]

Related

.htaccess SEO friendly URL with queryString

I have a page like:
page.php?mode=dashboard&hl=en
and i want to use like:
site.com/dashboard?hl=en
how must i use RewriteRule? Thanks...
EDIT: I think this is not possible with .htaccess. Need to use javascript.
wish it helps you
# Redirect /Category.php?Category_Id=10&Category_Title=some-text
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^Category_Id=(\d+)&Category_Title=([\w-]+)$
RewriteRule ^Category\.php$ /Category/%1/%2? [R=301,L]
You may be able to use this code in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /page\.php\?mode=([^\s&]+)&(hl=[^\s&]+) [NC]
RewriteRule ^ /%1?%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w-]+)/?$ page.php?mode=$1 [L,QSA]

How to redirect in order to get a clean url

I would like to redirect http://example.com/video.php?id=123A_BC to http://example.com/video/123A_BC in .htaccess file by using mod_rewrite.
You need to use rewrite instead of redirecting for clean url.
Please try this,
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)$ $1.php?id=$2 [QSA,NC,L]
The following code solved my problem
RewriteCond %{REQUEST_URI} ^/video\.php$
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^(.*) video/%1? [R=301,L]

How do I rewrite and use only one version of the url?

I have the following rewriterule:
RewriteRule ^first-test-page/?$ testing.php?idPage=1 [NC,L]
How do I redirect the user to domain.com/first-test-page/ if he goes to domain.com/testing.php?idPage=1 ?
Thank you!
You need a two rules. This should work for you. Give this a try.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /+testing\.php\?idPage=1
RewriteRule ^ /first-test-page? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^first-test-page/?$ /testing.php?idPage=1 [NC,L]

htaccess RewriteRule with parameters to specific location

I have a page which has a link as:
http://www.example.com/listings.php?packageType=luxury-travel
Which I'd like to send to:
http://www.example.com/luxury-travel
And also:
http://www.example.com/listings.php?packageType=luxury-travel&packageTypeSub=remote-luxury-retreats
To go to:
http://www.example.com/luxury-travel/remote-luxury-retreats
I have used:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /listings.php?packageType=$1&packageTypeSub=$2 [L]
Which ends up affecting other pages which I have such as:
http://www.example.com/about-us
I have also tried placing:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Before. Also tried playing with:
RewriteCond %{QUERY_STRING} ^packageType=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &packageType=([^&]+) [NC]
RewriteRule ^([^/]*)$ /listings.php?packageType=$1 [L]
Which just give me a 404 error on both links. Anyone have any ideas? Would have thought this would have been a simple one!
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^\.*]+)/([^\.*]+)/?$ listings.php?packageType=$1&packageTypeSub=$2 [L,QSA]
RewriteRule ^/?([^\.*]+)/?$ listings.php?packageType=$1 [L,QSA]
Try this:
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+)&packageTypeSub=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,NC,L]
RewriteRule ^([^/]+)/([^/]+)?$ /listings.php?packageType=$1&packageTypeSub=$2 [L]
Note that if only packageType is available, the rules would redirect to http://www.example.com/luxury-travel/ (note the / at the end) to help differentiate it from links like http://www.example.com/about-us.
If that's not an option, then you would have add rules that specifically provide page/link names.
I have edited and removed some of the development code as it wasn't working.
After a lot of headscratching I finally found a solution which works:
# Rewrite specific pages - listings
# Allow packageType
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+) [NC]
RewriteCond %{QUERY_STRING} !&packageTypeSub=([^&]+)
RewriteRule ^ /%1/? [R=301,NC,L]
# Allow packageTypeSub
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+)&packageTypeSub=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,NC,L]
# Ignore the images folder allowing images to display
RewriteCond %{REQUEST_URI} !(upload-images) [NC]
RewriteRule ^([^/]+)/([^/]+)?$ /listings.php?packageType=$1&packageTypeSub=$2 [L]
The main issue was the upload-images folder which contains all the images for the site packages was being blocked. I managed to exclude this folder from the rewrite rules and it now seems to be working. Thanks all for your help. Hope this helps someone else in future.

.htaccess RewiteCond (QUERY_STRING) with 2 variable and rewrite properly

I need to rewrite my url from:
http://***.com/index.php?cat=VAR&page=1
to:
http://***.com/VAR/1
With 301 redirection.
I got this so far:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index.php\?
RewriteCond %{QUERY_STRING} ^cat=(.*)\&page=(.*)
RewriteRule . /%1/%2 [R=301]
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(files|admin)/
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L]
But the first 3 rules doesn't seems to work at all. (I'm a beginner in htaccess)
How can i fix this problem? Thanks!
EDIT :
Thanks to Jassie, the solution:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index.php\?
RewriteCond %{QUERY_STRING} ^cat=(.*)\&page=(.*)
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(files|admin)/
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L]
change it to RewriteRule ^(.)/(.)$ /index.php?cat=$1&page=$2 [L,QSA] and try

Resources