.htaccess fails on HTTP GET with questionmark - regex

I have tried almost every possible "solution" that I could find online for this matter, including Stackoverflow.
I am trying to rewrite
http://example.com/index.php?view=search&q=something
to
http://example.com/search/?q=something
When searching for something, the searchform direct's you to
search/?q=this
But "/search/" is allread passed once so it has already used the questionmark
now i'm stuck with the script redirecting every search with a questionmark while it should be an "&" sign.
It works if I manualy change the ? to &.
Can someone pleas help me make it work with the Questionmark?
Here is a copy of my .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
ErrorDocument 404 /test.php
RewriteRule ^nothingfound/ test.php [L]
RewriteCond %{QUERY_STRING} q=([^&]+)
RewriteCond %{QUERY_STRING} v=([^&]+)
RewriteRule ^search/([^/]+) /index.php?view=search&q=%1
RewriteRule ^([^/.]+)/?$ index.php?view=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?view=$1&task=$2 [L]
RewriteRule ^([^/.]+)/([0-9]+)/([^/.]+)*/$ index.php?view=$1&id=$2&title=$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([0-9]+)*/$ index.php?view=$1&task=$2&page=$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/*/$ index.php?view=$1&task=$2&letter=$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([0-9]+)*/$ index.php?view=$1&task=$2&letter=$3&page=$4 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([0-9]+)/*/$ index.php?view=$1&id=$2&title=$3&page=$4 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/*/$ index.php?view=$1&id=$2&title=$3&list=$4 [L]
Thanks in advance.

This rule should work for you:
RewriteCond %{QUERY_STRING} (^|&)[qv]=.+ [NC]
RewriteRule ^(search)/?$ index.php?view=$1 [QSA,L,NC]

Is this what you're looking for?
Replace this
RewriteCond %{QUERY_STRING} q=([^&]+)
RewriteCond %{QUERY_STRING} v=([^&]+)
RewriteRule ^search/([^/]+) /index.php?view=search&q=%1
with this
RewriteCond %{QUERY_STRING} \bq=
RewriteRule ^search/$ index.php?view=search [QSA,L]

Related

.htaccess RewriteRule with two different conditions

I have two different URLs:
1.: mysite.com/file.php
2.: mysite.com/**articles**.php?article=**something**
I would like to rewrite mysite.com/file.php to mysite.com/file.
AND
I would like to rewrite mysite.com/**articles**.php?article=something to mysite.com/**news**/**something**
I tried this but it's not working.
It rewrite mysite.com/file.php to mysite.com/file, but does not rewrite mysite.com/**articles**.php?article=**something** to mysite.com/**news**/**something**
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?\ ]+)\.php
RewriteCond %{REQUEST_FILENAME} !exception.php
RewriteRule ^news/([^/]+)(/*)$ /articles.php?article=$1
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
(There is one exception in the code: exception.php which I don't want to rewrite.)
Can you please correct my code? I've been working on this for 4 days, I've read everything, but I can't do it.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect for file.php file.
RewriteCond %{REQUEST_URI} !/?exception\.php [NC]
RewriteCond %{THE_REQUEST} \s/(file)\.php/?\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite for php files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]
##external + internal rewrite rules for articile.php here.
RewriteCond %{THE_REQUEST} \s/articles\.php\?article=(\S+)\s [NC]
RewriteRule ^ /news/%1? [R=301,L]
RewriteRule ^news/([^/]*)$ articles.php?article=$1 [NC,QSA,L]

.htaccess RewriteRule keeping URL structure

My current rewrite rule:
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?a=$1&v=$2&id=$3 [L]
The result above works great so I can format the URL like
domain.com/a/b/c
I would like to add in a domain switch as well so the results I want is
sub.domain.com/a/b/c when you access it using domain.com/a/b/c
Currently here is what I have tried
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?a=$1&v=$2&id=$3 [L]
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule (.*)$ http://sub.domain.com/ [R=301,L]
But the result of this is
http://sub.domain.com/a=a&v=b&id=c
and needs to be
http://sub.domain.com/a/b/c
Thanks for the help!!
Reverse the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule (.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)?$ index.php?a=$1&v=$2&id=$3 [L,QSA]
Make sure to test this after clearing your browser cache.

REQUEST_URI RewriteCond Syntax

I am sure I am doing something silly here, but I can't seem to figure out what it is.
I can't understand why the RewriteCond for the {REQUEST_URI} isn't evaluating to true and thus preventing the RewriteRule from firing. Instead, given the following url, the rule fires and the user is always taken to https://www.diffsite.com.
Any help would really be appreciated - and thanks in advance.
url:
http://www.regularsite.com/page-name/
.htaccess code:
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (iphone|ipod|android|symbian|windows\phone|blackberry|iemobile|opera\ mobile|palmos|webos|googlebot-mobile) [NC]
RewriteCond %{REQUEST_URI} !^page-name/ [NC]
RewriteRule ^(.*)$ https://www.diffsite.com/ [L,R=302]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
%{REQUEST_URI} always matches a URI that starts with leading forward slash / so this will work for you:
RewriteCond %{REQUEST_URI} !^/page-name/ [NC]
However in your rule RewriteCond %{REQUEST_URI} is not even needed as you can do this negation in RewriteRule itself:
RewriteCond %{HTTP_USER_AGENT} (iphone|ipod|android|symbian|windows\phone|blackberry|iemobile|opera\ mobile|palmos|webos|googlebot-mobile) [NC]
RewriteRule !^page-name/ https://www.diffsite.com/ [NC,L,R=302]
Not that inside .htaccess leading slash is not matched in RewriteRule.

Rewrite subdomain exactly like the main website

I'm currently working on a blog portal, and I'm trying to achieve a specific thing here.
This is my .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php?page=$1&page2=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?page=$1&page2=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php? page=$1&page2=$2&page3=$3
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?page=$1&page2=$2&page3=$3
When a person is trying to reach SOMETHING.mydomain.com, htaccess is rewriting it to blogdisplay.php?page=BLOGNAME. Just as planned. Now, the problem is that I want it to behave exactly like the main website and rewrite SOMETHING.mydomain.com/page to blogdisplay.php?page=BLOGNAME?page2=page.
Is there any way to make the subdomain use blogdisplay.php instead of index.php, but otherwise work exactly like the subpages of the "main website"?
Replace your code with this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
## don't do anything
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/?$ blogdisplay.php?page=%1&page2=$1 [L,QSA]
RewriteRule ^([a-z0-9-]+)/?$ index.php?page=$1 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2&page3=$3 [L,NC]

url rewriting in .htaccess

I am trying to serve different urls using mod_rewrite but whatever I try it is just not working.
An example url would be
http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles
And I am trying to change the url using .htaccess to:
http://www.site.com/country/tours/dynamic-part/new-york,los-angeles
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Any ideas? I though I was close but not anymore :/
The RewriteRule does NOT match the query string, see
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched
So the .+city part of the rule will never match.
This should work tho...
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]
The subsitution can read back-referenecs to the RewriteCond pattern.