URL rewrite and RegExp - regex

I want the URL rewrite to result in this:
test.php?site=hi --> www.domain.com/hi
test.php?site=hi&id=1 --> www.domain.com/hi/1
test.php?site=hi&cmd=test --> www.domain.com/hi/test
test.php?site=hi&cmd=test&id=1 --> www.domain.com/hi/test/1
I have tried this, but it doesn't work. I am new at regular expressions, so there might be place for corrections:
RewriteRule ^([a-zA-Z]+)/?+([0-9]+)?/? test.php?site=$1&id=$2
RewriteRule ^([a-zA-Z]+)/?+([a-zA-Z]+)?/?+([0-9]+)?/? test.php?site=$1&cmd=$2&id=$3
How should the RewriteRule be?
Thanks in advance.

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 /
RewriteRule ^([^/]+)/([a-z]+)/(\d+)/?$ /test.php?site=$1&cmd=$2&id=$3 [L,NC,QSA]
RewriteRule ^([^/]+)/([a-z]+)/?$ /test.php?site=$1&cmd=$2 [L,NC,QSA]
RewriteRule ^([^/]+)/(\d+)/?$ /test.php?site=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ /test.php?site=$1 [L,QSA]

Related

How to modrewrite in my subfolder

I have a PHP script on a subfolder of my server and it generates url like this:
http://example.com/subfolder//index.php?a=profile&u=username
Instead I would like a SEO friendly url like:
http://example.com/subfolder/profile/username.html
I'm on Apache and mod_rewrite is enabled, so I edited the .htaccess of my subfolder page:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ //index.php?a=$1&u=$2 [L]
But it doesn't work.
This is my full .htaccess code:
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
RewriteRule ^([^/]*)/([^/]*)\.html$ //index.php?a=$1&u=$2 [L]
What's wrong with my code?
Use this rule in /subfolder/.htaccess:
RewriteEngine on
RewriteBase /subfolder/
RewriteRule ^([^/]+)/([^/]+)\.html$ index.php?a=$1&u=$2 [L,QSA,NC]

Htaccess rewrite url not working

I have created a .htaccess file to rewrite url. But when i opened the url which should be rewrited by the htaccess the url was not changed. Here is my .htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule seller/username/(.*)/ seller.php?username=$1
RewriteRule seller/username/(.*) seller.php?username=$1
Please help me out as i am beginner to htaccess. Thanks in advance
Have it this way:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+seller/\.php\?username=([^\s&]+) [NC]
RewriteRule ^ /seller/username/%1? [R=302,L]
RewriteRule ^seller/username/(.+)/?$ seller.php?username=$1 [L,QSA]

GET request htaccess redirection

I need to redirect url from
website.com/search.php?city=india&keyword=laptop
to
website.com/search/india/laptop
How can i do this with htaccess
Present code in my htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)(/)?$ search.php?city=$1&keyword=$2
You can use this rule in your root .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+search\.php\?city=([^&]+)&keyword=([^\s&]+) [NC]
RewriteRule ^ /search/%1/%2? [R=301,L,NE]
RewriteRule ^search/([\w-]+)/([\w-]+)/?$ /search.php?city=$1&keyword=$2 [L,QSA]

.htaccess ReWrite wildcard folder paths from host

My desired result is change a file path as a direct decedent from root, where there are N numbers of paths.
For example:
www.host.com/a/b/c/e/f/g/images/1.jpg, where A~G is not always given.
Result:
www.host.com/images/1.jpg
This is what I have so far:
www.host.com/a/images --> www.host.com/images
Using: RewriteRule ^a\/images/$ images/$1 [L]
What I need is a wildcard in front of /images/
Like this: RewriteRule ^(.*)\/images/$ images/$1 [L]
I have also tried: RewriteRule ^(.*)/images/$ http://www.host.com/images [R=301,L]
What is the proper way to write this?
Can you try this rule:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^.+?(/images/.*)$ $1 [L,NC]
RewriteRule ^index\.php$ - [L]

why the .htaccess file can't rewrite the dynamic url in my website

the current is:
http://www.vitalimaging.com/about.php?page=overview_vision
want to clean the url, and here is my code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^about/([A-Za-z0-9-]+)/?$ about.php?page=$1 [L]
why it's not working?
Your example also has underscore in query string but your regex in RewriteRule doesn't include _. Simplify your regex like this:
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 /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(about)\.php\?page=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteRule ^about/([^/]+)/?$ /about.php?page=$1 [L,QSA,NC]