Regular expression for Apache url rewrite .htaccess - regex

I would like to rewrite this:
www.mywebsite.com/i.php?photo=1234
to
www.mywebsite.com/i/1234
Can anyone help me with the right regular expression for .htaccess

Try this:
RewriteRule ^i/([0-9]+)/?$ i.php?photo=$1 [NC,L]
Remember to turn the rewrite engine on before you declare the rule:
RewriteEngine On

The first thing you need to do is make sure Multiviews is turned off, since you have a URI that is /i/ as well as a script that is called /i.php. Multiviews and mod_negotiation will bypass mod_rewrite and your rules will get skipped.
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteRule ^i/([0-9]+)/?$ /i.php?photo=$1 [L,QSA]
And for redirecting links that are outside of your control:
RewriteCond %{THE_REQUEST} \ /+i\.php\?photo=([^&\ ]+)
RewriteRule ^ /i/%1/ [L,R=301]

Related

Fix the regex code in .htaccess for beautifying this url

my current regex code to beautify a url (www.abc.com/user.php?url="rio") in .htaccess file is
RewriteCond %{REQUEST_URI} user
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)$ user.php?url=$2
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/$ user.php?url=$2
It works fine and the page url is beautified to :
www.abc.com/user/rio
but there is a problem now, Now even if I change url like in following ways:
www.abc.com/user/user/user/rio
www.abc.com/user///rio
www.abc.com/user/abc/rio
These links work same way, and that is making content duplication.
Please help!
You may try these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# remove multiple slashes
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]
# handles /user/something
RewriteRule ^user/([\w-]+)/?$ user.php?url=$1 [L,QSA,NC]
Options -MultiViews is required to turn off content negotiation feature.

A single rewriterule matches 2 different cases

I have below 2 urls to redirect,
example.host.com/index.php
host.com/example/index.php
when the server is under maintenance, I would like to redirect them to:
example.host.com/maintenance.php
host.com/example/maintenance.php
with help of .htaccess
I know how to achieve them separately, but now I would want a single writerule to do the job for me.
Below is what I wrote in .htaccess, but not work as expected.
Options FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.php$ [NC]
RewriteRule (.*?)/([^/]+)/?$ $1/maintenance.php [R=307,L]
Any help or idea why it's not working?
You can use this single rule:
Options FollowSymlinks
RewriteEngine on
# generate dynamic rewrite base var
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteRule !maintenance\.php$ %{ENV:BASE}maintenance.php [R=307,L,NC]

.htaccess file I can't make it working

Disclaimer : I dont know anything about URL rewriting
currently my URL looks like
legalHQWithNewAddressTable/legalHQ/public/admin/Parties.php?caseid=7 (any number basically)
But how can I make it to looks like the following
legalHQWithNewAddressTable/legalHQ/public/admin/Parties/caseid/7/
I'm trying but not working RewriteRule ^([a-zA-Z0-9]+)/?
Please dont make things complicated I dont know anything about URL rewriting so please keep it as simple as possible.
Any Idea?
Try adding these rules to the htaccess file in the /legalHQWithNewAddressTable/legalHQ/public/ folder:
RewriteEngine On
RewriteBase /legalHQWithNewAddressTable/legalHQ/public/
RewriteCond %{THE_REQUEST} /admin/([A-Za-z0-9]+)\.php\?caseid=([0-9]+)&?([^\ ]*)
RewriteRule ^ admin/%1/caseid/%2/?%3 [L,R]
RewriteRule ^admin/([A-Za-z0-9]+)/caseid/([0-9]+)/ admin/$1.php?caseid=$2 [L,QSA]
Place this rule in your /legalHQWithNewAddressTable/legalHQ/public/admin/.htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /legalHQWithNewAddressTable/legalHQ/public/admin/
RewriteRule ^Parties/caseid/([0-9]+)/?$ Parties.php?caseid=$1 [L,QSA,NC]

mod_rewrite rule to change parent folder

Some unchangeable hyperlinks on a website point to /folderA/index.php?id=somestuff.
I need to redirect the Request to /folderB/index.php?id=somestuff instead.
I did some experimenting with this but I just can't get it to work, any help is appreciated.
RewriteEngine on
RewriteRule /folderA/index\.php\?id=([\w-]+)$ /folderB/index.php?id=$1
Keep in mind that RewriteRule doesn't match query string and matches only REQUEST_URI without it. Use this code instead:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folderA/(index\.php\?id=[^&\s]+) [NC]
RewriteRule ^ /folderB/%1 [R=302,L]
Assuming that you are not planning on using different get request this should work:
RewriteEngine on
RewriteRule /folderA/index\.php\?id=(.*?)$ /folderB/index.php?id=$1

mod_rewrite rewrite_rule syntax question

I have the following in my .htaccess currently-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^go/([^/]*)/([0-9]+)/([0-9]+)/?$ http://foo.com/wp-content/plugins/foo/cloak.php?post_id=$2&link_num=$3&cloaked_url=$0 [L]
RewriteRule ^go/([^/]+)[/]?$ http://foo.com/wp-content/plugins/foo/cloak.php?name=$1&cloaked_url=$0 [L]
</IfModule>
I want to change it so that the domain 'http://foo.com' is auto-detected and inserted (or just left off if it's unnecessary.
I'm hoping to use this .htaccess to manage multiple mapped domains to the same code base and can't really have it work with a specific URI in there.
Use RewriteCond for that:
RewriteCond %{HTTP_HOST} !^foo.com$
RewriteRule ...