htaccess to append a query string to all URLs - .htaccess

I need to append /?test=1 to every single URL on the website.
My current solution that is not working:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^$ /?test=1 [L,NE,R=301]
Can someone please tell me what I'm doing wrong.

What you ask in your question contradicts partly what you show in your attempt. So this is just a guess, more leaning to what you actually ask:
RewriteEngine on
RewriteCond %{QUERY_STRING} !(?:^|&)test=1(?:&|$)
RewriteCond ^ %{REQUEST_URI}?test=1 [QSA,END]

Related

htaccess deny access to URIs via regular expression

I want to deny access to all my posts/pages via their database id address e.g. /?p=1 , /?p=2 , ... etc
I am looking at doing this using .htaccess and would guess it would be best done with a RewriteCond rule. I have tried this but it doesn't appear to work.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?p=*
RewriteRule ^(.*)$ - [F,L]
Any help greatly appreciated
With your shown samples, please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^p=[0-9]+ [NC]
RewriteRule ^ - [F,L]
As per OP's comments to match p or page with = digits try following then.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^(p|page)=[0-9]+ [NC]
RewriteRule ^ - [F,L]

RewriteRule for htaccess note working

I'm trying to get pages that call a variable (from my OLD site) to point to the root level of my blog... using my htaccess file. My knowledge of the htaccess syntax, isn't very good...
For example, any of these pages...
www.example.com/blog/?paged=100
www.example.com/blog/?paged=84
www.example.com/blog/?paged=3
... will ALL be rewritten as ...
www.example.com/blog/
I tried...
RewriteEngine on
RewriteRule ^blog/paged=?$ $1/blog/$2 [R=301,L]
... but that only works for paged=1. Any other variable didn't work (i.e. 2, 100). Can someone help with the correct way to do this?
To be able to match query string you need to use RewriteCond like this.
If /blog/.htaccess doesn't exist:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^paged=\d+
RewriteRule ^blog/?$ /blog/? [R=302,NC,L]
If /blog/.htaccess exists:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^paged=\d+
RewriteRule ^/?$ /blog/? [R=302,NC,L]

PHP RewriteRule detect if url contain "string" and replace

This may be a basic question regarding RewriteRule but I just counldn't make it work.
What I want to do is detect urls like:
mydomain/myapp/id/some/random/url.html?param=somethingThatIdoUse
mydomain/myapp/id/other/randomabc/perrito.php?param=somethingThatIdoUse
...
The part that I need to discart is from the /id/[all this]?param=somethingIdoUse and use the param if is possible or send the complete url as param so I can regex to get the param.
And have a rule that detect that /id/ exist and redirect to something like:
mydomain/myapp/other/manageRequest.php?params=somethingThatIdoUse
(the params I could get the whole url and strip it as string is no problem)
As well the application have different modules like:
mydomain/myapp/moduleOne/index.php
mydomain/myapp/moduleTwo/index.php
This have to keep working the same.
As far I've tried some of them like:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET /.*;.* HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://localhostdev/app/index.php %{REQUEST_URI}? [R=301,L]
RewriteEngine On
RewriteBase /spt/
RewriteCond %{REQUEST_FILENAME} !^id$ [NC]
RewriteRule ^(.*)$ index.php [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} !/campaings/response\.php$
RewriteRule ^/something/(.*) /other/manageRequest.php? [L]
But nothing seamed to do kind of what I needed.
Thanks in advice
mydomain/myapp/id/some/random/url.html?param=somethingThatIdoUse
Here is an example using the above URL:
RewriteRule ^id/some/random/url.html/? /myapp/other/manageRequest.php [L,NC]
The query will be passed through unchanged to the substitution URL
well actually end up being really basic it worked with:
RewriteRule ^.*id.*$ handleRequest.php [NC,L]
I do get the params as they are sent!

How to use mod_rewrite

I have a site (sports.com, let's say) and I'd like to set up mod_rewrite so that a visitor to "football.sports.com/america" is shown content from "sports.com/stuff/place.php?id=america". I'd still like visitors to the naked "sports.com" to see "stuff/index.php".
Sorry, I'm a real newb with this. I've tried and failed several times, heres what I have so far...
IndexIgnore *
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} football\.sports\.com [NC]
RewriteRule ^([^/.]+)$ stuff/place.php?id=$1 [L]
RewriteRule (.*) stuff/index.php?%{QUERY_STRING}
Any help would be massively appreciated.
You can do this like:
RewriteRule ^america$ america/ [NC]
RewriteRule ^america/$ stuff/place.php?id=america [NC,L]
Redirects football.sports.com/america[/] to football.sports.com/stuff/place.php?id=america. This is a static rule: it won't work with france for example.
If you need a generic way you need :
RewriteRule ^([^/.]+)$ $1/ [NC]
RewriteRule ^(.*)/$ stuff/place.php?id=$1 [NC,L]
To get naked site to be redirected, add:
RewriteRule ^$ stuff/index.php [L]
Redirects football.sports.com/ to football.sports.com/stuff/index.php
Eventually you can add the query string of necessary.

.htaccess mod_rewrite playing with variables

I want to have my site urls look like
http://example.com/place/info?var=info&morevars=ifneeded
Place and info are also variables but they have a fixed name, the ones after would vary. EDIT This is the url I am trying to rewrite
http://example.com/test.php?place=test&action=info&var=info&morevars=ifneeded
This is what I have so far
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC]
I think there a way to do this with {QUERY_STRING} but I can't get it to work just 500 errors or it don't make a differences.
You have set the QSA flag that automatically appends the original requested query to the new one:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC,QSA]
You're missing the first /
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/ test2.php?place=$1 [NC]
RewriteRule ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/ test2.php?place=$1&action=$2 [NC]

Resources