Silent Rewrite Rule - regex

I want to make this redirection:
www.domain.com/es/j/j1.html => www.domain.com/j/j1.html?lang=es
To do this I have created an .htacces in the folder /es/j with the following code:
RewriteEngine On
RewriteRule ^j(.*)$ www.domain.com/j/j$1?lang=es
It works, but the thing is that I need to do it "silently", I mean, the url in the browser must show www.domain.com/es/j/j1.html instead of www.domain.com/j/j1.html?lang=es
How can I do that?

If you want to have a silent rewrite then use this code.
In root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^es/(j/.+?\.html)$ /$1?lang=es [L,NC,QSA]

Related

RewriteRule when there is ?id=

I try to write a RewriteRule in the .htaccess but I have problems
I try to redirect from:
blog/entrada.php?id=2
To:
/blog/3D-touch
This is one of the multiple things I tried and does not work:
RewriteRule ^blog\/entrada\.php\?id=2$ /blog/3D-touch [L,R=301]
What is wrong with my Rule. How to redirect effectively?
Thanks
Querystring is not part of match in RewriteRule directive, to redirect query strings, you need to use RewriteCond one of the following options :
option 1
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/entrada\.php\?id=2 [NC]
RewriteRule ^ /blog/3D-touch? [NC,L,R]
option 2
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=2$ [NC]
RewriteRule ^blog/entrada\.php$ /blog/3D-touch? [NC,L,R]
We use an empty question mark ? at the end of the target url to discard the old query strings, otherwise these query strings get appened to the target url by default.
Change the R to R=301 if you want to make the redirection permanent.
use from this code
RewriteRule blog/(.*) blog/entrada.php?id=$1
this code will redirect all urls which have blog/ to blog/entrada.php and put after value of blog/ to $_GET['id']
you should have following code n the top location of your htaccess file
Options +FollowSymLinks
RewriteEngine on

Remove last Path from URL using .htaccess

I need to create a RewriteRule on my .htaccess file which removes the Path ("page-2") if it's included at the end of the URL. So for example:
http://myhost.com/path/page-2
should redirect to:
http://myhost.com/path
I've found a similar solution on SO:
RewriteEngine On
RewriteRule ^([^/.]+)/?$ /$1/page-2/ [L]
However it does not work for me. No redirection happens.
Any help?
You need to have this rule:
RewriteEngine On
RewriteRule ^(.+?)/page-2/?$ /$1 [L,NC,R=301]

How to rewrite this URL using .htaccess

I am basically just trying to write a .htaccess redirect that turns the url
www.example.com/Artist/Profile?Artist='test' to www.example.com/Artist/test
without actualy redirecting the user.
use the following directive
RewriteCond %{QUERY_STRING} (^|&)artist='([^&]+)' [NC]
RewriteRule ^Artist/Profile$ /Artist/%2? [L,NC]

Can't match RewriteRule

I need to do a very simple URL rewrite but my RewriteRule is not working.
I want this URL: http://myweb.com:8080/MySite/bla?bla
To become this: http://myweb.com:8080/MySite/index.php
My .htaccess file content is like this:
RewriteEngine On
RewriteRule bla\?bla index.php
It is located in "MySite" folder. I can do other url-rewriting rules with success but I got stuck whenever I need to write a rule with question mark inside.
What am I doing wrong?
You need to use the %{QUERY_STRING} rewrite condition for this.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^bla$
RewriteRule ^/?bla$ index.php [NC,L]
Please, note that the ? in the rewrite rule is not there to match against the ? in the query string. That part is handled completely by %{QUERY_STRING}. The [NC] just makes the rule case-insensitive and [L] marks the rule as last.

Tricky URL rewrite, regex .htaccess

I have a tricky issue redirecting some URLs internally for my site.
The situation is this, I currently have a URL like example.com/check/youtube.com which internally redirects to check.php?domain=youtube.com using the following mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteRule ^offline offline.php [NC,L]
RewriteRule ^error error.php [NC,L]
RewriteRule ^check/(.*)$ check.php?domain=$1 [NC,L]
However I would also like to be able to redirect to check.php using a URL like example.com/youtube.com. Unfortunately it is just beyond me to figure it out.
I have a directory /assets/ with all the CSS, JS, etc. which shouldn't be affected.
Thanks
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.]+\.[^/]+$ check.php?domain=$0 [L]
This rule rewrites any request with a URL path of the form [^/.]+\.[^/]+ (a string that contains at least one dot but no slashes at all) that cannot be mapped to an existing file to your check.php.
As you want to redirect "example.com/youtube.com" does that mean you wish to redirect pretty much anything? What is specifically allowed to be passed, e.g. would I be allowed to pass "example.com/youtube.com/foobar.php" for a redirect to check.php?