htaccess add query to the end of the link - .htaccess

I need to know how to redirect
http://test.com/test.php to http://test.com/test.php?var=foo
& also if it's http://test.com/test.php?var=foo to be http://test.com/test.php?var=foo&var2=foo
If this will cause a loop just tell me how to achieve it and I am going to test
Thanks in advance

You can use these rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^.]+\.php)$ /$1?var=foo [L]
RewriteCond %{QUERY_STRING} (?:^|&)var=foo(?:&|$)
RewriteRule ^([^.]+\.php)$ /$1?var2=foo [L,QSA]

Related

How can I add conditional url variable using htaccess?

I use following code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)post_type=product(&|$)
RewriteCond %{QUERY_STRING} !(^|&)product_cat=xyz
RewriteRule ^(.*)$ http://www.example.com/?%{QUERY_STRING}&product_cat=xyz
but it doesn't works.
I need to add variable "product_cat=xyz" where query_string contains "post_type=product" and it doesn't just contains "product_cat=xyz".
Examples:
If url is "http://www.example.com/?post_type=product&product_cat=xyz" then htacces leaves it unchanged.
If url is "http://www.example.com/?post_type=product" then htacces must add "&product_cat=xyz"
:-) Can you help me, please?
Thanks in advance.
You can use:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)post_type=product(&|$)
RewriteCond %{QUERY_STRING} !(^|&)product_cat=xyz(&|$)
RewriteRule ^ http://www.example.com/?product_cat=xyz [QSA,L]

Restrict link with htaccess file

I need to restrict access to any link that contains the word user.
My rewrite rule looks like this
RewriteRule (^|/)user(/|$) - [F,L]
It works fine when http://sitename.com/user is used but does not work for http://sitename.com/?q=user
Can anyone suggest a fix for this ?
Thanks in advance
You'll have to check the request uri and the query string separately like this:
RewriteEngine On
RewriteCond %{REMOTE_HOST} !^123\.456\.789\.123$
RewriteCond %{REQUEST_URI} ^/user$ [OR]
RewriteCond %{QUERY_STRING} ^q=user$
RewriteRule ^(.*)$ / [R=302,L]

.htaccess RewriteRule to get pretty links

I have URLs like:
1) http://www.example.com/?page=2
2) http://www.example.com/my-photos-folder?page=3
Here page number will increase sequentially (page=1, page=2, page=3 .....).
"my-photos-folder" Can be anything like "my-images-folder" or "Nice-photos" etc..
What i would like to get:
1) http://www.example.com/page/2
2) http://www.example.com/my-photos-folder/page/3
My .htaccess has the following rules:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^page\/(.*)$ index.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?kohana_uri=$1 [QSA,PT,L]
RewriteRule ^$ index.php?kohana_uri=$1 [QSA,PT,L]
RewriteRule ^index.php/(.*) $1 [QSA,R,L]
</IfModule>
Can any expert suggest me what rules i need to add so that my desired outcome will be achived. Thank you in advance.
Add theese lines after the RewriteEngine On line:
RewriteRule ^page\/(.*)$ index.php?page=$1 [L]
RewriteRule ^(.*)\/page\/(.*)$ $1?page=$2 [L]
#develroot
RewriteRule ^page\/(.*)$ index.php?page=$1 [L] //This is working fine for homepage
RewriteRule ^(.*)\/page\/(.*)$ $1?page=$2 [L] //this rule still not working on directory level.
The second one still have issue. Please consider the rules which are already there.[I have updated the question - added your first rule which work fine.]

how to redirect from modules.php?name=News&file=article&sid=XXX to domain.eu/XXX?

I would like to redirect all url requests with the phpnuke string modules.php?name=News&file=article&sid=XXX (xxx is a number) to my domain-name.eu/XXX . How is that possible?
i tried
RewriteCond %{QUERY_STRING} ^(.*&|)modules.php?name=News&file=article&sid=(&.*|)$
RewriteRule ^(.*)$ %1domain.eu/%2 [L,R=301]
but it has no effect. Where is the mistake?
Also i don't understand why a simple
RewriteRule ^(.*)$ /modules.php?name=News&file=article&sid=$1 [L,R=301]
doenst work. Any ideas?
Thanks a lot for any help.
Not sure why I'm seeing this in Drupal questions. But try this.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/modules\.php$
RewriteCond %{QUERY_STRING} ^sid=([0-9]*)$
RewriteRule ^(.*)$ http://domain.eu/%1 [R=301,L]
You can't match everything after the sid=(.*) regex part in the URL in most cases. modules.php?name=News&file=article&sid=XXX and modules.php?sid=XXX&name=News&file=article are the same in functionality.
Learned at http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/

Help with .htaccess RewriteRule. Need to take user from one URL to other

I want users who type
http://www.example.com/word-of-the-day
to be taken to
http://www.example.com/index.php?page=word-of-the-day
But I want
http://www.example.com/word-of-the-day
to be shown in the URL for the user.
What should I do in my .htaccess? I tried but the regular expression
and the syntax of RewriteRule is way too complicated for me to
figure out how to do it.
Any help will be appreciated.
Edit:
Also, how can I say this in htaccess -
if they type http://www.example.com/word-of-the-day, take them to http://www.example.com/index.php?page=word-of-the-day
or if they type http://www.example.com/something-else, take them to http://www.example.com/index.php?page=something-else
or else, just take them to the URL they typed.
The condition below checks that index.php is not being requested. If not apply the rule. This will work for any of the scenarios you listed above.
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L]
In response to your comment about only wanting to do this for a few specific pages, it would look like this(as an alternative to Nils edit):
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteCond %{REQUEST_URI} ^word-of-the-day$ [OR]
RewriteCond %{REQUEST_URI} ^something-else$ [OR]
RewriteCond %{REQUEST_URI} ^even-something-else$
RewriteRule ^(.*)$ index.php?page=$1 [L]
Try this
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
Or more flexible
RewriteEngine on
RewriteRule ^(.*)$ index.php?page=$1
Not tested, yet it sould work.
To your edit:
Just define those specific URLs manually:
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
RewriteRule ^word-some-example$ index.php?page=some-example
RewriteRule ^some-other$ index.php?page=some-other

Resources