Put a parameter after url suffix - .htaccess

Using htaccess or any other way how can I pass a parameter to end of url suffix
EX: I need all the .html to be html?v=1
I tried the following
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.html?v=1 [R]
But Its not working

Check first that the query string does not already exist:
RewriteEngine On
RewriteCond %{QUERY_STRING} !v=1
RewriteRule ^(.*)\.html$ $1.html?v=1 [R,L]

Related

Htaccess redirect issue with query string

I have URL domain.com/index.php
I have written this code to redirect to clientarea.php
RewriteEngine On
RewriteRule ^index.php /clientarea.php [R=301]
Now this works except for urls that contain a query string (eg. domain.com/index.php?=something). This will also redirect but I don't want it when there is a query string.
Can anyone tell me how I can do it ?
To prevent rewriting when a query string is there
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ /clientarea.php [R=301,L]
RewriteEngine On
RewriteRule ^index.php$ /clientarea.php? [R=301]
$ marks the end of the string in the regex.

HTAccess redirect Based on Query Parameter Fails?

am trying to redirect (HTAccess) a URL based on the existance of a Querystring Paramater "ref". Only redirect when this exists with the prefix folder of "TMP" and page name "domain.html".
The problem is that the redirect is working, but the Querystring isnt being passed on
e.g.
http://www.olddomain.com/TMP/domain.html?ref=website-reference.com
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/TMP/domain\.html$
RewriteCond %{QUERY_STRING} ^ref=([0-9]*)$
RewriteRule ^(.*)$ http://www.newdomain.com/?ref=%1 [R=301,NE,NC,L]
You can use:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^ref=
RewriteRule ^TMP/domain\.html$ http://www.newdomain.com/ [R=301,NE,NC,L]
With the same query string.

How to redirect a page with a query string to the homepage?

Recently I redesigned my site let's say http://www.sitename.com/ .
Before the redesign, the homepage url was something like this: http://www.sitename.com/default.asp?id=1&lg=1
Old pages had also weird query strings and they are not relevant any more, so, I want to redirect everything that begins with default.asp to the homepage.
RewriteRule default\.asp.* \
alternatively
RewriteCond %{QUERY_STRING} ^default.asp?id=1&lg=1$ [NC]
RewriteRule http://www.sitename.com/ http://www.sitename.com/? [R=301,L]
This is the closest I have got so far, but I am pretty sure its wrong.
Can you help?
Update: This is my whole .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule default\.asp.* /?
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I have tried putting my rule at the top and at the bottom, (no luck). Should I embed it somehow on the other rule?
You can use these rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# redirect /default.asp to landing page
RewriteRule default\.asp$ /? [L,NC,R=301]
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Based on this site, if you set up the rule like this
RewriteRule default\.asp.* /?
it should work.
Here is the reference for how you can replace query strings in the rewrite:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
Modifying the Query String
By default, the query string is passed through unchanged. You can,
however, create URLs in the substitution string containing a query
string part. Simply use a question mark inside the substitution string
to indicate that the following text should be re-injected into the
query string. When you want to erase an existing query string, end the
substitution string with just a question mark. To combine new and old
query strings, use the [QSA] flag.
UPDATE
Based on your comment, try this:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule default\.asp.*$ /? [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Note the [L] at the end of RewriteRule default\.asp.*$ /? [L] which terminates the rewriting process when that match is found. See L flag
If you don't include the L flag, then the process will continue with the rest of the rules in your .htaccess until it reaches the end with no matches or until it matches one with the L flag. Think of it like a switch statement, which needs a break in each case or else it continues to the next case, if it helps you.
default.asp is a filename you need to match against it using %{REQUEST_URI} variable. it's not part of querystring. To redirect requested URIs that contain default.asp ,you can use the following :
RewriteCond %{REQUEST_URI} /default\.asp [NC]
RewriteRule ^ http://sitename.com/? [R,L]

url rewrite preserve get variables

I am looking to rewrite
http://domain.com/zzz-zzz/?source=[variable1]&referrer=[variable_2]
to
http://domain.com/xxx-xxx/?source=[variable1]&referrer=[variable_2]
I had done :
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)zzz-zzz(.*)$
RewriteRule ^$ ?xxx-xxx%2 [R=301,L]
The QUERY_STRING does not include the path and script file (zzz-zzz/). That would be the REQUEST_URI. The QUERY_STRING would be source=variable1&referrer=variable2. If all you want to do is change zzz-zzz to xxx-xxx:
RewriteEngine On
RewriteBase /
RewriteRule ^zzz-zzz/?(.*)$ /xxx-xxx/$1 [R=301,L]
ought to get you close. The entire Query String should be brought over.

Htaccess redirection, redirect loop

I would like to redirect mysite.com/index.php to mysite.com/index.php?id_category=12&controller=category
Here is my htaccess file.
But it doesn't work. Chrome says : This webpage has a redirect loop.
Options +FollowSymLinks +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteRule index\.php http://mysite.com/index.php?id_category=12&controller=category
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://www.mywebsite.com/index.php?foo=bar [R=301,L]
The first condition checks if the URI is equal to index.php and the second one checks if GET values are empty. The AND between the 2 conditions is implicit here.
Probably you should change use another file name instead of index.php for the destiny. But you can try:
RewriteRule index.php index.php?id_category=12&controller=category

Resources