I want to have a formatted URL like this:
https://www.example.com/view.php?id=1
where:
"id" range: 1-99 (without leading "0" from 1 to 9)
"id": always lowercase, no "Id" or "ID" or "iD"
Anything different from this format must redirect it to
www.example.com/view.php
Examples of bad URLs:
https://www.example.com/view.php?iD=1
https://www.example.com/view.php?Id=1
https://www.example.com/view.php?ID=1
https://www.example.com/view.php?id=1aaa
https://www.example.com/view.php?id=av1102
https://www.example.com/view.php?id=07
https://www.example.com/view.php?id=100
So far I have this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/view.php$
RewriteCond %{QUERY_STRING} ^(?!(id=[1-9][0-9]{0,1})$).*$
RewriteRule .* /view.php? [R=301,L]
But it doesn't work, either doesn't redirect or I receive well known "TOO MANY REDIRECTS" loop.
Please, help.
You can use this :
RewriteEngine on
RewriteCond %{QUERY_STRING} .+
RewriteCond %{QUERY_STRING} !^id=([1-9]{1,2}|[1-9a-zA-Z]+) [NC]
RewriteRule ^.*$ /view.php? [L,R]
Make sure to test this in a new browser or clear your browser cache before testing this.
Related
I'm trying to rewrite an url from:
https://www.website.com/test/test-detail?name=abc
to:
https://www.website.com/test/abc
I can't seem to find a way to keep only the value part of the query string.
This is the code:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test/test-detail/
RewriteCond %{QUERY_STRING} name=
RewriteRule ^(test)/(test-detail)/$ /test/$3 [R=301,L]
For the input:
https://website.com/test/test-detail/?name=abc
The result is:
https://website.com/test/?name=abc
Link to the example.
You can use this :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test/test-detail/
RewriteCond %{QUERY_STRING} name=(.+)$
RewriteRule ^(test)/(test-detail)/$ /test/%1? [R=301,L]
Make sure to clear your browser's cache before you test this.
i have a issue with some rewriting url with htaccess.
i need the following:
old: mydomain.com/categorie/details.php?companyid=10
new: mydomain.com/item/?p=10
where i want to catch and replace the numbers (of several id) dynamically.
I've read the answer of the other question but that's only replace the query string. I quess i need some regex but i'm a newbee with that..
i have this in htaccess:
RewriteCond %{REQUEST_URI} ^/categorie/details\.php$
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$
RewriteRule ^(.*)$ www.mydomain.com/item/?p=%1/ [R=301,L]
Before i migrate i've tested this and it worked fine, for some reasons it won't work anymore after migration.
Do i miss something? Is this the wrong approach?
Kind regards,
Tom.
At this line
RewriteRule ^(.*)$ www.mydomain.com/item/?p=%1/ [R=301,L]
Put the ful URL like this :
RewriteRule ^(.*)$ http://www.example.com/item/?p=%1/ [R=301,L]
Also put this [NC] ,nocase to accept upper-case and lower-case, at the end of this line
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC].
So , your rules should look like this :
RewriteCond %{REQUEST_URI} ^/categorie/details\.php$
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/item/?p=%1 [R=301,L]
To sumerize all these rules more , do this :
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^categorie/details\.php$ http://www.example.com/item/?p=%1 [R=301,L]
And as you menthioned that it is same domain , let them be like this :
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^categorie/details\.php$ /item/?p=%1 [R=301,L]
No need to full URL.
Note: clear browser cache then test
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!
I have this link: http://www.domain.com.mk/lajmi.php?id=2790,
and i want to change it to http://www.domain.com.mk/lajmi/2790
With this code I can change the link to /lajmi/2790 but i get 404 error.
I mean i get the link
http://www.domain.com.mk/lajmi/2790, but it has 404 error (i dont se the content)
This is my code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
What I am doing wrong ?
Try this one :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1&r=0 [L]
(the &r=0 in the final rule is for not getting an infinite loop)
Single direction rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L,QSA]
This means that every uri of kind /lajmi/2790 will be passed to /lajmi.php?id=2790 in a sub-request.
However, in this case, if the user hits /lajmi.php?id=2790 by himself, then this is the url he will see in the browser, not the "beautified one".
Bi-directional rewrite:
RewriteEngine On
RewriteBase /
; Redirect lajmi.php?id=2790 to a beutified version, but only if not in sub-request!
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{IS_SUBREQ} !=true
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ lajmi/%1 [R=301,L]
; Make the beutified uri be actually served by lajmi.php
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L]
Here, an additional RewriteCond was added to the first rule checking that this is not a sub-request, to ensure that the rules do not loop.
You can pick which way you like, but the first approach is enough if you build the links in your HTML in the 'beautified' way already (no need to redirect the browser twice just to see the page).
I am trying to get Apache to redirect /a.php?a=123 to /b/123 (where 123 could be any number between 1 and 9999) but can't seem to get it to work.
This is what I have in htaccess:
RewriteEngine on
RewriteRule ^a.php?a=([0-9]+) /b/$1 [L]
RewriteRule ^a.php$ /c/ [L]
With this going to a.php?a=123 results in 404, but going to just a.php works as expected.
I tried escaping the ? (RewriteRule ^a.php\?a=([0-9]+) /b/$1 [L]) but it still doesn't work.
What am I doing wrong please?
The query string is not part of the URI path that is tested in the RewriteRule directive. This can only be tested with a RewriteCond directive:
RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^a\.php$ /b/%1? [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^a\.php$ /c/ [L,R]
But if you want it the other way (requests of /b/123 are redirected to /a.php?a=123):
RewriteRule ^b/([0-9]+)$ a.php?a=$1 [L]