How do i create only ONE rewrite line in htaccess - .htaccess

I have similar friendly url's like this:
http://localhost/galeria
http://localhost/galeria/
http://localhost/galeria/cat
http://localhost/galeria/cat/
I want to rewrite ONE rule in my htaccess file. So far, i was able to create 2 rules, like this:
RewriteRule ^galeria/?$ gallery/newest.php [L]
RewriteRule ^galeria/cat/?$ gallery/newest.php [L]
How do i create only ONE rewrite line for my 4 fiendly url's?
Thanks

You can use this single rule:
RewriteRule ^galeria(/cat)?/?$ gallery/newest.php [L,NC]
(/cat)? makes /cat optional thus matching all 4 URLs
RegEx Demo

Related

RewriteRule translating url to use different php files

In the .htaccess file of my PHP application I use one simple rewrite to translate my URLs from:
/shop/hats/detroit/
to:
index.php?url=/shop/hats/detroit/
using the following rewrite rule:
# Rule 1
RewriteRule ^(.*)$ /repos/nvp/httpdocs/index.php?url=/$1 [L,QSA]
I would like the following rewrite to work as well:
# Rule 2
RewriteRule ^pic/(.*)$ /repos/nvp/httpdocs/get_image.php?pic=$1 [L,QSA]
But the two rules are kind of overwriting or conflicting with each other. They work each on their own but not together. How can I use all urls to use Rule 1 and only if url starts with pic/ to use Rule 2 instead of Rule 1?
This is because the pattern (.*) matches all uris .
In order to avoid the rules overriding ,you need to reorder your rules and put the specific rules first in order.
# specific rules
RewriteRule ^pic/(.*)$ /repos/nvp/httpdocs/get_image.php?pic=$1 [L,QSA]
#catch-all rules
RewriteRule ^(.*)$ /repos/nvp/httpdocs/index.php?url=/$1 [L,QSA]

.htaccess rewrite rule, change URL

I'm looking for the following to change the URL with two variables in a .htaccess file, from:
domain.com/folder/a123/url/index.php
domain.com/otherfolder/a321/url/index.php
to:
domain.com/folder/url/index.php
domain.com/otherfolder/url/index.php
I would prefer it in a single line .htaccess RewriteRule, if possible.
"folder" and "otherfolder" are the only two "folder names" and "a123"/"a321" can be anything starting with "a" and ending with random numbers.
You can do that in one with just one rule using this:
RewriteEngine on
RewriteRule ^(.+)/a[0-9]*/url/index.php$ /$1/url/index.php [R=301,NC]
But if you want to redirect all pages and not only index.php then use this:
RewriteRule ^(.+)/a[0-9]*/url/(.*)$ /$1/url/$2 [R=301,NC]
Also, if you want to rewrite the URLs instead of redirect them change [R=301,NC] to [NC].
Note that if you have that pattern in other URLs this rule will also apply, for example it will also redirect domain.com/adifferentfolder/a567/url/index.php to domain.com/adifferentfolder/url/index.php, if you have those type URLs and don't want to redirect them you need to use two rules instead:
RewriteRule ^folder/a[0-9]*/url/(.*)$ /folder/url/$1 [R=301,NC]
RewriteRule ^otherfolder/a[0-9]*/url/(.*)$ /otherfolder/url/$1 [R=301,NC]

htaccess rewrite rewrite url with folders after php file

i am trying to get something like this:
http://localhost/Engine/admin.php/users/manage
to rewrite to something like this:
http://localhost/Engine/admin.php?p=users&a=manage
i am a beginner with rewrite rules, but i have a basic one here, they cant conflict...
RewriteEngine on
RewriteBase /
RewriteRule ^([^.]*)$ /Engine/index.php?page=$1 [QSA,L]
also, if anyone has any improvements for my other rewrite, it would be most helpfull, it rewrites
http://localhost/Engine/somepage
to:
http://localhost/Engine/index.php?page=somepage
Your generic rule that matches everything should be listed as last rule. You can improve it by only allowing to match if .php is not in the url. For admin.php you know how many arguments there should be, so you can simply match the static part of the url with ^Engine/admin\.php/, then all arguments with ([^/]+) (match everything but /). The last /? matches a trailing slash that might or might not be appended.
For more information read the documentation.
RewriteRule ^Engine/admin\.php/([^/]+)/([^/]+)/?$ /Engine/admin.php?p=$1&a=$2 [L]
RewriteCond %{REQUEST_URI} !^/Engine/.*\.php.*$
RewriteRule ^Engine/(.*)$ /Engine/index.php?page=$1 [QSA,L]

how to attach multiple parameter to index.php using rewrite rule?

hi i am trying to make clean and neat url using rewrite rule
I want to achive :
abc.com/t/param1/param2
Rewite rule that I wrote
RewriteRule ^t/(.+)/(.+)$ t/index.php?v=$1&t=$2 [L]
but it doen't work it redirects to :
http://abc.com/?v=param1&t=param2
Your regex should only match characters that aren't slashes. So your rule should look like
RewriteRule ^t/[^/]+/[^/]+$ t/index.php?v=$1&t=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /?v=$1&t=$2 [L]

.htaccess url search and replace one string for another

I need to change all URL's of one pattern to another:
http://www.example.com/index.php?option=com_fireboard
to
http://www.example.com/index.php?option=com_kunena
how would I substitute like this?
Try adding this somewhere appropriate in your htaccess file in your document root (probably before your joomla rules if you have any)
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)option=com_fireboard(.*)$
RewriteRule ^/?index\.php$ /index.php?%1option=com_kunena%2 [L]

Resources