Apache Rewrite with variables in Query String - .htaccess

I have a two part problem that I have only been successful with the first part.
I have the following listed in .htaccess which works great:
RewriteRule ^senior/?$ demo.php?dID=1 [NC,L]
So visitors can go straight to mysite.com/senior and the correct internal page (demo.php?dID=1) gets pulled up.
My problem is that I also would like a rewrite where /demo.php?dID=1 shows up in the URL bar as /senior. So existing links show up with the new user friendly url.
My attempt so far has been:
RewriteCond %{QUERY_STRING} ^dID=1$
RewriteRule ^demo.php$ senior [NC]
RewriteRule ^senior/?$ demo.php?dID=1 [NC,L]
Thanks for your time and help.

You want to match against the request instead of the query string and redirect the browser:
RewriteCond %{THE_REQUEST} \ /demo\.php\?dID=1($|\ |&)
RewriteRule ^demo.php$ /senior? [NC,R]
RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L]

Place this additional rule before your current rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+demo\.php\?dID=([^\s&]+) [NC]
RewriteRule ^ /senior? [R=302,L]
RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L]
Your current rule based on QUERY_STRING will loop since internal rewrite rule will populate the QUERY_STRING and both rule will keep triggering each other.

Related

.htaccess forcefully hide home page uri

I have the following .htaccess code:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^$ subpage [L]
So basically if someone visit "www.example.com," he'll see the content of "www.example.com/subpage" without the url changed. This is good.
However, they can still visit the page by "www.example.com/subpage." If that happens, I want the url changed back to "www.example.com."
Is it possible? What I've tried so far gave me redirection loop.
You need an additional rule that matches against the actual request and not the URI. Since the rewrite engine loops, the URI keeps changing, so you need to match against the %{THE_REQUEST} variable. You need this rule before the rule that you have in your question:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \ /+subpage(\?|\ |$)
RewriteRule ^ / [L,R=301]

infinite loop while using modwrite in htaccess

What am trying to do in that code is:
Any visitor click on an href like http://domain.com/article.php?id=44 the .htaccess will transfer him to domain/article/44
then another rewrite rule to to get the content of requested id from the file article.php
RewriteCond %{QUERY_STRING} ^id=([^&]*)
RewriteRule ^article\.php$ http://alarabe.org/article/%1?
RewriteRule ^article/([a-z0-9\-]+)$ /article.php?id=$1 [L]
This rule isn't right and will certainly cause looping. You need to use THE_REQUEST variable that represents original request received by Apache from your browser.
Try this instead:
RewriteCond %{THE_REQUEST} \s/+article\.php\?id=([^\s&]+) [NC]
RewriteRule ^ http://alarabe.org/article/%1? [R=302,L]
RewriteRule ^article/([a-z0-9\-]+)$ /article.php?id=$1 [L]

(htaccess) Rewrite English names of php files to their Dutch equivalents

I would like to rewrite the English names of php files to their Dutch equivalents.
For example: someurl.com/news.php?readmore=4#comments should become someurl.com/nieuws.php?leesmeer=4#kommentaar. The code from news.php should be executed but nieuws.php should be in the url the arguments should function as well.
I tried several htaccess examples but I can't get it to work.
Any help would be appreciated.
Edit: Working progress from answers below and final solution.
RewriteCond %{QUERY_STRING} ^readmore=(.*)$
RewriteRule ^news.php$ nieuws.php?leesmeer=%1 [R=301,L]
RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^news.php$ nieuws.php [R=301,L]
RewriteRule ^nieuws.php$ news.php?norewrite [QSA]
RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^search.php$ zoeken.php [R=301,L]
RewriteRule ^zoeken.php$ search.php?norewrite [QSA]
# make sure rewrite is activ
RewriteEngine On
# Rewrite a request for nieuws.php to news.php
RewriteRule ^nieuws.php$ news.php
Should do the trick.
Instad you could send all requests to an index.php and parse them there:
## Redirect everything to http://hostname/?path=requested/path
RewriteEngine On
RewriteRule ^([\w\W]*)$ index.php?path=$1 [QSA]
[QSA] makes sure you get the original get arguments too.
Now you have to parse the request in $_GET['path'] in you index.php and include the requested page.
eg:
if ($_GET['path'] == 'nieuws.php') {
include 'news.php';
} else if (empty($_GET['path'])) {
echo "HOME";
}
if you want to make make the user always sees nieuws.php in its address bar, even if he requested news.php, you could try the following:
RewriteEngine On
# Redirect news.php to nieuws.php if and only if the request comes from the client
# (suppose the client didn't set ?norewrite.)
RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^news.php$ nieuws.php [R=301,L]
# Send news.php if nieuws.php was requested and prevent news.php from being redirected
# to back to nieuws.php by the rule above.
RewriteRule ^nieuws.php$ news.php?norewrite [L,QSA]
(R=301 means send a "moved permanently" redirect to the client, L means stop rewriting after this rule matched)
The hole thing with norewrite (you could use something else instead) is only needed to avoid an endles loop of rewriting between news and nieuws.
To translate the GET arguments, you can try the following code before the first line of the above code:
RewriteCond %{QUERY_STRING} ^readmore=(.*)$
RewriteRule ^news.php$ nieuws.php?lesseer=%1 [R=301,L]
Things after a the # in an url can't be changed in .htaccess, since they aren't send to the server at all. The only chance to change them is using JavaScript. (See lots of question here on manipulating them within JavaScript)
Try:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news\.php
RewriteRule ^ /nieuws.php [L,R=301,QSA]
RewriteRule ^nieuws\.php$ /news.php [L,QSA]

301 redirecting pagination with get variables

I'm trying to set 301 redirects on pages that have 'page=1' in the URL to stop duplicate content issues.
e.g.
http://www.domain.com/reviews/?page=1
to
http://www.domain.com/reviews/
I've tried all of the variations I can find and can't seem to get anything to work.
RewriteRule ^reviews(/)?page=1$ http://www.domain.com/reviews/ [L,R=301]
RewriteRule ^(.*)/?page=1 http://www.domain.com/reviews/ [R=301,L]
RewriteCond %{QUERY_STRING} page=1
RewriteRule ^reviews$ http://www.domain.com/reviews/ [R=301,L,NE]
None of these have worked. I'm not really sure what else to try.
There are multiple different sections of the site that I need to do this for:
reviews
news
videos
accessories
hardware
An overall solution to redirect all ?page=1 URLs to their relevant section would be best.
Use this code to redirect every URI with ?page=1 to one without the query parameter:
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
Or else if you want to redirect ONLY /reviews URI then
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^(reviews)/?$ /$1? [R=301,L]
the question is already answered, i just would like to mention that your rules are not working because you didn't append a trailing ? to the new url in the rewrite rule

Specific .htaccess redirect based on query_string

I need this logic to work:
I want rewrite this string for users to see
http://mysite.com/index.php?cl=mykeystring
to
http://mysite.com/otherkey/
http://mysite.com/index.php?myvar=test&cl=mykeystring&mysecondvar=morevalue
to
http://mysite.com/otherkey/myvar=test&mysecondvar=morevalue
But when http://mysite.com/otherkey/ is written, so load
http://mysite.com/index.php?cl=mykeystring, but no redirects will be done.
Is it possible? There are no possibility to change anything in codes, but only .htaccess
This logic is nearly realized by this code:
RewriteCond %{QUERY_STRING} ^(.*?)cl=mykeystring(.*?)$ [NC]
RewriteRule ^index.php$ /otherkey/%1%2? [R,L]
RewriteRule ^otherkey/(.*?)$ /index.php?cl=mykeystring&$1
but im getting some not needed amp symbols on first rewrite rule. any solutions?
I think you can do something like this:
RewriteCond %{QUERY_STRING} cl=mykeystring [NC]
RewriteRule ^index.php /otherkey/ [QSA]
Docs here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Resources