Use .htaccess to replace after the first slash but keep query - regex

I have the following link:
http://example.net/invite/12345
I need to replace it to http://example.net/wedding/index.php?main_page=invite&invite_code=12345
I've got the following....but I'm just not hitting the nail here.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /wedding/index.php?main_page=invite&invite_code=$1 [L,QSA]
It's taking me to:
http:///example.net/wedding/index.php?main_page=invite&invite_code=invite%2F12345
Thanks for the help!

$1 is a reference to the capturing group (.*), so:
RewriteRule ^invite/(.*)$ /wedding/index.php?main_page=invite&invite_code=$1 [L,QSA]

Related

mod rewrite is not working and not giving any error

We want to change links "www.example.com/page/login/" to "www.example.com/sayfa/giris". I tried this: RewriteRule ^page/^(.+[^/])$/ /sayfa/^(.+[^/])$ [L] But nothing is change. Is there any error?
Thank you very much for your response #geert3 and #anubhava. But all trying failed. Here is my .htaccess content:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule /(uploads/.*) $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule /(uploads/.*) $1 [R=301,L]
RewriteRule ^sayfa/giris/?$ /page/login [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
Make sure there is not .htaccess in /sayfa/ folder.
I think you need to remove the second ^ and add an initial slash after the first ^. The slash after $ must be removed.
^/page/(.+[^/])$
^ at the start of an expression means "beginning of line" (i.e. in your case you want /page in front of the path). After a square bracket it means "none of the listed characters" (i.e. in your case no slash). In other places, it makes no sense.
$ means "end of line" so it makes no sense to add stuff after it.
Also as #Croises noted, you can't have "matches" on the right. Right side is your target URL, so there you can either re-use matches you made on the left side, use environment variables or hardcoded text, but no new matches. So no ^, [], $ etc.
So for instance the whole rule would become:
^/page/(.+[^/])$ /sayfa/giris [L]

URL rewriting regex with optional capturing

I have these types of URLs:
/manage/123
/manage_foo/456
/manage_bar/789
/manage
/manage_foo
/manage_bar
I need to rewrite to:
/manage.php?id=123
/manage_foo.php?id=456
/manage_bar.php?id=789
/manage.php?id=
/manage_foo.php?id=
/manage_bar.php?id=
How do I make the capturing of the id optional? I know this works if an id is present:
RewriteCond %{REQUEST_URI} ^\/(manage.*)\/(.*)$
RewriteRule ^ %1.php?id=%2 [NC,L]
That regex breaks down if you remove the slash before the id
You can use this rule:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(manage)(?:/([0-9]*))?/?$ $1.php?id=$2 [QSA,L,NC]

Using mod rewrite with wildcards for dynamic creation of URLs

I have the following:
RewriteRule ^tribute-acts/([^/.]*)/?$ /entertainment/act-view.html?act_name=$1 [L,QSA,NC]
which changes:
http://mysite.com/entertainment/act-view.html?act_name=elvis
to:
http://mysite.com/tribute-acts/elvis
Works great, but what if the tribute-acts part was dynamically generated and different for each link? Therefore endless possibilities...
Can I use a wildcard instead of tribute-acts/ ?
I tried the following, using (.*), but it didn't work:
RewriteRule ^(.*)/([^/.]*)/?$ /entertainment/act-view.html?act_name=$1 [L,QSA,NC]
You can try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/([^/]*)/?$ /entertainment/act-view.html?act_name=$1 [L,QSA]

I can not understand apache rewrite which use $1 in rewriteCond

for example:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
I can not undertand the meaning of $1 in the RewriteCond.
If $1 appoint to (.*) of RewriteRule,so we can use like that in the RewriteCond?
Yes $1 corresponds to first matching group in your RewriteRule. This is usually done to prevent infinite looping (dreaded internal 500 error in Apache).
PS: This condition may not be necessary here since you already have RewriteCond %{REQUEST_FILENAME} !-f before it.

htaccess - multiple rewrites & capture groups

I'm trying to handle multiple areas of an application, but the URLs are not being rewrited as expected.
This is the .htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^division/(.*)/section/(.*)$ ./index.php?division=$1&section=$2
RewriteRule ^division/(.*)$ ./index.php?division=$1
RewriteRule ^area/(.*)$ ./index.php?area=$1
What's expected:
If the URI matches division/some_division/section/some_section rewrite to index.php?division=some_division&section=some_section
If no section (section/some_section) is defined, go to the second rule -
If the URI matches division/some_division rewrite only to index.php?division=some_division
If no division is defined, and the URI matches area/some_area rewrite to index.php?area=some_area
I'm almost sure I can combine the two first rules, I've tried this regex but it didn't work:
^division/(.*)( /section/(.*) )?$
It supposed to make /section/some_section an optional value.
Unfortunately nothing works. Any ideas?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^division/(.*)/section/(.*)$ ./index.php?division=$1&section=$2 [QSA,L]
RewriteRule ^division/(.*)$ ./index.php?division=$1 [QSA,L]
RewriteRule ^area/(.*)$ ./index.php?area=$1 [QSA,L]