htaccess-rewrite-rule for more than one parameter - .htaccess

I use the following rule
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
for rewriting
www.mysite.com/123
to
www.mysite.com/index.php?action=123
This needs to remain like this, but there are cases where I also need to rewrite
www.mysite.com/123/abc
to
www.mysite.com/index.php?action=123&example=abc
How can I achieve this? Thanks!

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 ^([^/]+)/([^/]+)/?$ index.php?action=$1&example=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?action=$1 [L,QSA]

Related

.htaccess RewriteRule slash to get parameter

I just transfered my Domain to a new Server. Mod_Rewrite is enabled on the new server but unfortunately some RewriteRules don't work, while others do. I haven't changed anything in the .htaccess
So the URL www.mydomain.com/go/10.html should make an internal redirect to www.mydomain.com/go.php?name=10
The snippet in the .htaccess looks like this:
# go.php
RewriteRule ^go$ "$0/" [R=301,L,QSA]
RewriteRule ^go/$ go.php [L,QSA]
RewriteRule ^go/.*?([^\.\/]*)\.html$ go.php?name=$1 [L,QSA]
The $_GET["name"] is not available if I call this url.
Replace your .htaccess code with this.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(go)/([^.]+)\.html$ /$1.php?name=$2 [L,QSA,NC]

.htaccess Remove one of GET values and redirect

url: http://www.side.com/en/page-1/
I need to redirect to http://www.side.com/page-1/
How to do this using .htaccess file, maybe call php file and parse string( URI )?
You can do that with a simple rule:
RewriteEngine On
RewriteRule ^.*?/(.*)$ /$1 [L,R=301]
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 ^en/(.*)$ /$1 [L,R=301,NC]

Pull querystring from cgi-bin with mod_rewrite and redirect

I have a file path that looks like this: /cgi-bin/folder/script.cgi?t=1&u=http://domain.com/url/url-2/
What I want to do is create a rewrite rule to pull the querystring u= and then redirect to that URL, I've been working for a while on this with no success.
RewriteRule ^cgi-bin/folder/script.cgi?t=1&u=(.*)$ $1 [R=301,L]
Above is an example of one of my "solutions" that obviously didn't work. Any help is much appreciated. Thanks!
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/+cgi-bin/folder/script\.cgi\?t=1&u=([^\s&]+) [NC]
RewriteRule ^ /%1 [R=302,L,NE]

Having much trouble with mod_rewrite

I'm having some problems in my .htaccess file.
I would like to display the content of this URL:
http://mywebsite.com/admin/payments/invoices/view/index.php?id=123456
When I access this one:
http://mywebsite.com/admin/payments/invoices/view/123456/
Here's my actual .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /admin/payments/invoices/
RewriteRule ^/view/([0-9]+)/(index.php)?$ /view/index.php?id=$1 [L,QSA]
Do you have any idea?
Thanks!
If you do have the view directory, then the easiest thing is to put this .htaccess in that directory (i.e. {DOCUMENT_ROOT}/admin/payments/invoices/view/.htaccess):
Options +FollowSymlinks
RewriteEngine On
RewriteBase /admin/payments/invoices/view
RewriteRule ^(\d+)$ index.php?id=$1 [L,QSA]
The index.php in the left side hand of the RewriteRule is not required (actually, I expect it not to work: the DirectoryIndex should not yet have been injected in the URI at this stage - unless some other RewriteRule is in effect?), nor is the / at the end of the RewriteBase.
I tested the above on Apache/2.2.21, but the module rules are the same for later versions.
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*admin/payments/invoices/view/index\.php\?id=([0-9]+) admin/payments/invoices/view/$1/index.php [L]
</IfModule>
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)id=([0-9]+)
RewriteRule ^admin/payments/invoices/view/index\.php$ /admin/payments/invoices/view/%1/index.php [L]
So when you enter http://mywebsite.com/admin/payments/invoices/view/index.php?id=123456 in your browser's URL address bar, you will get served the content at /admin/payments/invoices/view/123456/index.php. Since it's completely unclear what you actually want, in case you wanted it the other way around:
RewriteEngine On
RewriteRule ^admin/payments/invoices/view/([0-9]+)/(index.php)?$ /admin/payments/invoices/view/index.php?id=$1 [L,QSA]

.htaccess rewrite url to arguments

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^products/([a-zA-Z]+)/([0-9]+)/$ index.php?product=$1&price=$2
This code makes
http://www.example.com/products/computer/1000
into
http://www.example.com/index.php?product=computer&price=1000.
Is it possible to make
http://www.example.com/scriptName/arg1/val1/arg2/val2/arg3/val3
into
http://www.example.com/scriptName.php?arg1=val1&arg2=val2&arg3=val3?
The user should be able to give an unlimited number of arguments. This way one can have /index.php?page=forum&subforum=subforum&quote1=postNo1&quote2=postNo43 and so on rewrite /index/page/forum/subforum/subforum/quote1/postNo1/quote2/postNo43
How would the .htaccess code look?
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 ^([^/]+)/([^/]+)/([^/]+)(.*?)/?$ $1/$4?$2=$3 [L,QSA]
RewriteRule ^([^/]+)/$ $1.php [L]
This will forward a URI of /scriptName/arg1/val1/arg2/val2/arg3/val3 to /scriptName.php?arg3=val3&arg2=val2&arg1=val1

Resources