URL rewriting via my .htaccess file - .htaccess

How do I change example.com/1 to example.com/?id=1
I've tried googling but I can only find code for example.com/?id=1 to example.com/1
I used a generator and got this, but it didn't work
RewriteEngine On
RewriteRule ^\?get\=$ / [L]
Thanks,
Isaac

It kind of depends on which way you want to do the rewrite - ie, what does the user type see, and what does the server do.
If you want the user to see "http://example.com/1" and internally the server provides "http://example.com/?id=1", then the following should work:
RewriteRule ^([0-9]+)$ /?id=$1
However, if you want the user see "http://example.com/?id=1", and internally the server provides "http://example.com/1", then the following, as per Jon Lin's answer, should do it:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^$ /%1?

You can't match against the query string in a RewriteRule statement, you need to use a RewriteCond and the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^$ /%1? [L]
The ? is needed in the rule's target to remove the query string.

Related

rewrite rules for utm_source parameter in url

guys, i have question on mod_rewrite function.
this is outside link like
http://example.com/article?utm_sources=baidu&utm_campain=alading
I like to change it to
http://example.com/article#utm_sources=baidu&utm_campain=alading
#utm_sources=xxx also can be traced by Google GA code
I try below code but not working
RewriteCond %{QUERY_STRING} ^utm_source(&.*)?$ [NC]
RewriteRule ^article$ /article?%1 [R=301,NE,NC,L]
how do i make rewrite rules for it? thanks
You can use this rule:
RewriteCond %{QUERY_STRING} ^utm_sources=.+ [NC]
RewriteRule ^article/?$ /article#%{QUERY_STRING}? [R=301,NE,NC,L]
Trailing ? is required to strip off previous query string.
I update with below rules it works for me, but thanks anubhava give answer.
RewriteCond %{QUERY_STRING} ^utm_(.*)?$
RewriteRule ^(.*)$ %{REQUEST_URI}#%{QUERY_STRING}? [R=301,NE,NC,L]

Dont redirect if parameter is set

I want to redirect a url except when there is a parameter named login with the value false.
Tested it on several regexp testers and it works like I want.
But when it comes to the server it doesn't.
I always get redirected to ?login=true.
Why?
RewriteRule (.*)write((?!/?login=false).)*$ ?login=true [L]
RewriteRule only matches against everything after the hostname and before the query string. You'll need to use RewriteCond and the %{QUERY_STRING} variable.
RewriteCond %{QUERY_STRING} !login=false
RewriteRule (.*)write$ ?login=true [L]
See the documentation for more information.
RewriteRule (.*)write$ ?login=true [QSA]
RewriteCond %{QUERY_STRING} !name=
RewriteRule .? - [S=1]
RewriteRule (.*)write$ ?login=true [L]
made it like this now
works for me

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!

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

.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