How to rewrite the get variables using htaccess? - apache

I have a htaccess file that rewrites the link
http://www.example.com/profiles.php?view_user=test
to
http://www.example.com/profiles?view_user=test
but i want to rewrite the remaining get variable --
profiles?view_user=test
to
profiles/view_user/test
help me out i dont know much of htaccess

Try adding these rules to the appropriate place in the htaccess file in your document root:
RewriteCond %{THE_REQUEST} \ /+profiles\.php\?([^=&]+)=([^&\ ]+)
RewriteRule ^ /profiles/%1/%2? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profiles/([^/]+)/([^/]+)/?$ /profiles.php?$1=$2 [L,QSA]
Also make sure you have multiviews turned off:
Options -Multiviews

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(profiles)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [QSA,L,NC]

Related

Can we write .htaccess for two different dynamic pages like product.php & blog.php in php mysql?

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?-([^/]+)? product-details.php?pro_slug=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?-([^/]+)? blog-details.php?n_slug=$1&n_id=$2 [L,QSA]
this will working for only first defined page product-details.php. please help.
I am hitting on www.test.com/blog/Want-to-transfer-your-home-loan-to-another-bank, www.test.com/product/agriculture-land-near-by-agra First url should be served by product-details and 2nd one should be served by blog-details.php file.
Based on your shown samples, please try following htaccess Rule file.
Make sure to clear your browser cache before testing your URLs.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(blog)/([\w-]+)/?$ product-details.php?pro_slug=$1&id=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(product)/([\w-]+)/?$ blog-details.php?n_slug=$1&n_id=$2 [NC,L,QSA]

.htaccess rewriting rule rewriting css files

I have a rewrite rule to make pretty url but its rewriting css, js and other files too.
I want to rewrite
http://localhost/example/post/?postid=af1ub4zu4y&title=this-is-the-first-post.html
to
http://localhost/example/post/af1ub4zu4y/this-is-the-first-post.html
I am trying this way
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/([^/]*)/(.*\.html) /example/post/?postid=$1&title=$2 [QSA,L,B]
also tried like these
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^post/([^/]*)/(.*\.html) /example/post/?postid=$1&title=$2 [QSA,L,B]
and
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
RewriteRule ^post/([^/]*)/(.*\.html) /example/post/?postid=$1&title=$2 [QSA,L,B]
But none of these solution are working for me. Please see and suggest any possible way to do this.
Try placing this rule in /example/post/.htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /example/post/
RewriteRule ^([^/]+)/([^/.]+\.html?)$ ?postid=$1&title=$2 [QSA,L,B,NC]

Convert dynamic urls to static for directory listener ("Encode Explorer")

I try to convert dynamic url to static for this directory listener: http://encode-explorer.siineiolekala.net/
Demo: http://encode-explorer.siineiolekala.net/explorer/index.php
The dynamic url is:
http://files.onitex.tmweb.ru/?dir=directory/subdirectory
What I want:
http://files.onitex.tmweb.ru/directory/subdirectory
I read documentation and wrote following rule in .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([\/\A-Za-z0-9-]+)/?$ /?dir=$1 [L]
It works fine in this tester http://htaccess.madewithlove.be/
but not on directory listener.
I think I need reversed rule.
Could you explain me, how I can solve this problem, please?
Update:
My current .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?)/$ /?dir=$1 [L,QSA]
Use this rule in root .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?)/$ /?dir=$1 [L,QSA]
Don't test it on htaccess.madewithlove.be, its not really reliable. Test it on your localhost.

Apache URL Rewriting issue

i've set the rules,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?userid=$1
it redirects http://localhost/username to http://localhost/user.php?id=username works fine,
but for other existing folder page still redirects to user.php page; not that existing folder, ex http://localhost/folder it still redirects to http://localhost/folder/?userid=folder,
how can it make it to work for existing directories, ex when i type http://localhost/folder then page should shown of /folder/ directory ???
Replace your .htaccess code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^(messages)/?$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(A-Z0-9_-]+)/?$ user.php?userid=$1 [L,NC,QSA]
If it still doesn't work please post matching long entries from access.log and enable RewriteLog in your httpd.conf and post logs from there in your question.
The problem is that your second RewriteRule has no RewriteConds. Your two RewriteConds only apply for the first rule, then they stop. The second rule therefore applies unconditionally to every request URI.
So this part of your .htaccess should look like this instead:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^messages/?$ index.php?page=messages [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?userid=$1 [L,NC,QSA]

.htaccess redirect and add variables

our clients banner campain lauched today an the wrong url has been deposited.
So i try to redirect
website.com/wheels/?lang=en&sel=540
to
website.com/wheels/index.html?lang=en&sel=540
I tryed to put a .htaccess in the folder wheels which contains following:
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/?$ index.html?lang=$1&sel=$2
But it doesn't work. Anyone has an idea, what i'm doing wrong?
EDIT
I found a existing .htaccess in the ROOT. It contains following code:
RewriteEngine On
RewriteBase /
RewriteRule ^(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)/ - [L]
RewriteRule ^typo3$ typo3/index_re.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php [L]
If I add the code folling code, nothing happens:
RewriteCond %{QUERY_STRING} ^lang=en&sel=[0-9]+(&|$) [NC]
RewriteRule ^(wheels)/$ /$1/index.html [R=301,L,NC]
Btw, lang is not static, it can be en or de.
Try adding this to the .htaccess in the root directory of your website. The querystring parameters are carried through by default.
RewriteEngine on
RewriteBase /
RewriteRule ^wheels/$ /wheels/index.html [R=301,L,NC]
Use this in your ROOT .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=en&sel=[0-9]+(&|$) [NC]
RewriteRule ^(wheels)/$ /$1/index.html [R=301,L,NC]
You could also do this by adding an extra RewriteCond in front of the existing RewriteRule
RewriteCond $0 !^wheels/