htaccess: Rewrite with several variables - .htaccess

I want to redirect a page like
/topic/italian-page2.html to /index.php?s=italien$p=2
or
/topic/france-page4.html to /index.php?s=france$p=4
but also (without page number)
/topic/spain.html to /index.php?s=spain$p=1
Right now I use
RewriteRule ^topic/([a-zA-Z0-9\-]+).html$ /index.php?s=$1&p=1 [L]
RewriteRule ^topic/([a-zA-Z0-9\-]+)-page([0-9]+).html$ /index.php?s=$1&p=$2 [L]
The first one (without page) works fine.
The second one doesn't.
It redirects to /index.php?s=italian-page2&p=$2
What did I do wrong? Where is my mistake?
Thank you!

Found a solution:
RewriteRule ^topic/([a-zA-Z0-9]*)-page([0-9]*).html$ /?s=$1&p=$2 [L]
RewriteRule ^topic/([a-zA-Z0-9\-]+).html$ /?s=$1&p=1 [L]
That works!

Related

.htaccess multiple redirects with fallback

I'd like to create some kind of redirection script using .htaccess to map short urls (like example.com/1 to other urls). To do so, I've created this:
RedirectPermanent /1 https://www.youtube.com/watch?v=...
RedirectPermanent /2 https://www.youtube.com/watch?v=...
So far, that's working. However, it's missing a fallback. I'd like to add a 404 page that is shown whenever someone tries to navigate a URL that doesn't have any redirect (yet).
I've tried adding this:
...
RewriteCond %{REQUEST_URI} !^/404.html$
RewriteRule .* /404.html [L,R=302]
Obviously, this isn't working, because now ALL calls are redirected to 404.html. I thought about adding the redirects as conditions, but since there might be many redirects that approach seems very bad to me.
What can I do instead?
Thanks for helping out.
I've done this now, but I'm not sure if this is the best solution.
Please comment if you have any advices.
RewriteRule ^/1$ https://www.youtube.com/watch?v=1 [L,R=301]
RewriteRule ^/2$ https://www.youtube.com/watch?v=2 [L,R=301]
RewriteCond %{REQUEST_URI} !^/404.html$
RewriteRule .* /404.html [L,R=302]

RewriteRule redirects to unexpected page

I am trying to change my url through .htaccess in the following way
Original URL: http://www.example.com/latest-news.php?id=2/topic=testing
Rewritten URL:http://www.example.com/2/testing
Rule for .htaccess
RewriteRule ^([^/]*)/([^/]*)$ /latest-news.php?id=$1&topic=$2 [L]
This is working fine but the other files which are existing in a folder are not opening. The url is opening as www.example.com/testing/foo.php but content of the page is of http://www.example.com/2/testing
Can you try with a similar rule like the one bellow?
RewriteRule ^([0-9]+)/([a-zA-Z])$ /latest-news.php?id=$1&topic=$2 [L]
Can't really test it on my server but you can adapt it to suit your needs
RewriteEngine On
RewriteRule ^([0-9]+)/([^/]*)$ /latest-news.php?id=$1&url=$2 [L]
Since second part consists characters and digits So I modify the answer of stoica. Thanks #stoica

.htaccess rewrite rule to change path to query string? [duplicate]

Ok, im pretty new at this and I would really appreciate some help, thanks!
How can i rewrite this in .htaccess correctly?
So I have a query string in my url:
/?url=contact
All i want to do is remove the query string
/contact
Help? I scoured google and I'm learning the syntax right now, but the fact remains..I dont know how to do it just yet. Thanks to all
This was my solution:
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Try this:
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [L]
To a user on your site, they will see and navigate to this:
http://example.com/contact
But the real page would be something like this:
http://example.com/index.php?url=contact
This bit, [L], tells the server that this is the last line of the rewrite rule and to stop.
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule index.html %1
(or whatever if it's not index.html, index.php, whatever)
You need to capture the query string, which is not looked at by RewriteRule normally, and use the %1 back reference, not $1 as you would in a capture in a RewriteRule
Before: https://example.com/index.php?user=robert
RewriteEngine On
RewriteRule ^user/([^/]+)?$ index.php?user=$1 [L,QSA]
After: https://example.com/user/robert

.htaccess 301 redirect problem

i try to redirect url's like:
example.com/video/1640/video-name
to
example.com/video/1640/video-name/
i've tried with:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
but it is not working
my currently htaccess file has only the first line:
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
and videos only loads at
example.com/video/1640/video-name/
url type
i want to redirect the non-backslash url type
example.com/video/1640/video-name
to the correct one (the one with the backslash)
How can i do this?
Your second rule should be RewriteRule ^video/([^/]*)/([^/]*)$ /video/$1/$2/ [R=301,L]
Or you could forgo the redirect totally, and just say RewriteRule ^video/([^/]*)/([^/]*)/?$ video.php?id=$1&title=$2 [L] which will allow both to view your video.
Update FallingBullets is right (see the comments on this answer), his answer better suites the OP's problem, so please ignore this answer (I am leaving it for reference, though).
Maybe you simply have to prefix your pattern with a /?? E. g.
RewriteRule ^/?video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^/?video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
# ^ these ones
instead of
RewriteRule ^video/([^/]*)/([^/]*)/$ video.php?id=$1&title=$2 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video/([^/]*)/([^/]*)/ [R=301,L]
since you are anchoring the pattern at the beginning of the path (using ^).

.htaccess redirecting dynamic URL

I am using the following code to attempt to redirect a dynamic URL to a new dynamic URL, under the same domain:
RewriteRule ^products/item/^\d([^/]+) /product/$1/ [R=301,L]
I've tried these too:
RewriteRule ^products/item/[^\d]([^/]+) /product/$1/ [R=301,L]
RewriteRule ^products/item/[0-9]([^/]+) /product/$1/ [R=301,L]
But this was redirecting /products/item/342/ to /product/42/, I tested this on a larger number (e.g. 123456789) and it redirected to /product/23456789/.
It would appear that my rule is not picking up the firsts digit, can anyone help me resolve this?
I've also tried using [\d] instaled of [0-9], but this has the same problem.
Cheers!
Try
RewriteRule ^products/item/(\d[^/]+) /product/$1/ [R=301,L]
RewriteRule ^products/item/([0-9]+) /product/$1/ [R=301,L]

Resources