RewriteRule on a long, ugly URL - .htaccess

i am trying to redirect http://domain.com/partners/index.php?00N600000028zDp=0016000000qp33G&00N60000002UzF5=0016000000qp33G&00N600000029PAh=0016000000qp33G&00N6000000292kr=GO%20GREEN%20AMERICA%20TV&00N60000002X3cq=1$
to another page. typically i would do this as such:
RewriteRule ^partners/index.php?00N600000028zDp...(shortening this)$ http://domain.com/page/ [L,NE,NC,R=302]
however this time it's not working. i suspect it has something to do with the ?,=,&, or % characters in the URL, but i am not sure. any advice? thank you.

You can't match against the query string (everything after the ?) in the regex pattern in a RewriteRule. You need to match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^00N600000028zDp=0016000000qp33G&00N60000002UzF5=0016000000qp33G&00N600000029PAh=0016000000qp33G&00N6000000292kr=GO%20GREEN%20AMERICA%20TV&00N60000002X3cq=1$
RewriteRule ^partners/index.php$ http://domain.com/page/? [L,NE,NC,R=302]
Keep in mind that when you redirect the browser, the browser will load http://domain.com/page/ and this redrect that you've setup doesn't touch that at all. So you'll see the contents of http://domain.com/page/ once you get redirected.

Related

Htaccess remove word and some string from url

I have discovered that some visitors to my site connect with a weird URL:
Example: http://www.example.com/article/?epik=3xFD_XXs
The trouble is, when "?epik=3xFD_XXs" is added to a URL, it causes me to lose ads on my website!
So, I would really like to find a solution.
I'm presuming that the solution would be to redirect them to the original URL, http://www.example.com/article/, whenever ?epik=Random_char is found.
Thank you.
This RewriteCond is to check whether the request query string contains epik=. If it contains the query string epik=, the RewriteRule redirect is then applied. The ? character is used to remove query string if any.
RewriteCond %{QUERY_STRING} epik=
RewriteRule ^ %{REQUEST_URI}? [R,L]

htaccess query string redirect not working correctly

I have a situation where I want to actually see the url variable even though the rest of my htaccess site uses readable URLS.
The issue is that it is simply showing up as a page not found...
This works...
RewriteRule ^files/(.+)/from_all_files/$ pages/file.php?slug=$1&from=all-files
This does not work
RewriteRule ^files/(.+)?from=all-files$ pages/file.php?slug=$1&from=all-files
Im looking for the second one to work.
You cannot check the query string in a RewriteRule, which can only see the REQUEST_URI. You need to use the following instead:
RewriteCond %{QUERY_STRING} ^from=all-files$ [NC]
RewriteRule ^files/(.+)$ pages/file.php?slug=$1 [QSA,L]
When you request http://example.com/files/some-file?from=all-files, the request will be internally rewritten to pages/file.php?slug=some-file&from=all-files. The Query String Append flag (QSA) will append the current query string to the one you're rewriting to.

URL Rewriting not possible?

I have a website with this url :
http://www.example.com/stage?dept=01
I want transform this url to this
http://www.example.com/stage-ain.html
I want that new url override the standard html and it's become the only url available (for SEO).
I do this, but it's not ok :
RewriteEngine On
RewriteRule ^stage?dept=01$ stages-ain.html [R=301]
Have you an idea ?
Thanks
The first argument of RewriteRule cannot match the query string. You have written a regex instead that matches the url stagedept=01 and stagdept=01.
You want to use a RewriteCond instead. You could use the %{QUERY_STRING} variable, but this will likely cause an infinite loop. Instead you probably want to match on %{THE_REQUEST}.
RewriteCond %{THE_REQUEST} /stage\?dept=01
RewriteRule ^ stages-ain.html [R,L,QSD]
Change the [R] flag to [R=301] when you have tested everything works as expected. Always use the L flag with external redirects, unless you have a very good reason to continue rewriting, as this can cause some weird problems.
See the documentation for more information.

How to write a htaccess to change url but point to actual path internally using regex match

url = www.example.com/de/abc
I just want to change fix url word 'abc' into fix word 'xyz' for browser compatibility only changed url show in browser. But change url point to actual directory path means
www.example.com/de/xyz will follow internally www.example.com/de/abc.
I used some regex to do this, but not able to get actual solution
How can I do this.
Thanks in advance
Code from comment :
RewriteCond %{REQUEST_URI} ^/de/advertiseWithUs$ [NC]
RewriteRule ^(.*) /de/unsere-tipps [R=302,L]
If I have correctly understood what you want, here a solution you can try :
RewriteRule ^de/advertiseWithUs$ http://yourdomain.com/de/unsere-tipps [R=302,NC,L]
RewriteRule ^de/unsere-tipps$ de/advertiseWithUs [NC,L]
The first rule change URL (with a 302 redirection), the second one make the internal redirection to point to the right page.

Pretty url doesn't show in url-bar

I have an htaccess file with several redirects, Now I want to create a pretty url for some link. I tried the following sentence and it does nothing:
RewriteEngine On
RewriteRule ^/example1.html /?page_id=100 [NC]
When I type www.MyDomain.com/?page_id=100 in my browser, the site shows, but the url still looks the same as I typed it. How would I change my sentence to show example.html in the url-bar instead of ?page_id=100 ?
Thanks in advance
You have a rule that says:
When the browser requests: /example1.html
Then show them: /?page_id=100
It goes one way, it doesn't do anything if the browser requests /?page_id=100. If you want to do something about a browser requesting the query string URL, *you need a rule to tell mod_rewrite to do it*:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?page_id=100($|\ |&)&?([^\ ]*)
RewriteRule ^ /example1.html?%3 [L,R=301]
Also, you may want to get rid of the / in the pattern of your rule:
RewriteRule ^example1.html /?page_id=100 [NC]
Leading slashes are stripped off when matching against rules in an htaccess file.

Resources