I want to add a parameter to a URL but currently it isnt showing in the $_GET global.
A snippet from my htaccess file is as below:
RewriteRule ^account/blogs/([0-9]+)/([^\s?]+)/?$ /account/blog.php?blogId=$1 [L,QSA]
Then in my php code i want to add a link such as:
/account/blogs/1/ThisIsWhereTheTitleGoes?delete=1
The wildcard (any char but space) option is for the blog title as i never know what it would be. I know want to add a query string param on the end such as ?delete=1. I however dont want this as part of the rewrite.
Does anybody know how to so this?
Thanks
My best bet would be to simply add get variables to the regex, like so:
RewriteRule ^account/blogs/([0-9]+)/([^\s?]+)/?(\?(.*))?$ /account/blog.php?blogId=$1&$4 [L,QSA]
This would rewrite
/account/blogs/1/ThisIsWhereTheTitleGoes?delete=1
to
/account/blog.php?blogId=1&delete=1
It would also support additional variables.
Related
I post here because despite the many topics on the net I have not managed to solve my problem.
I concise a website, and to optimize SEO, I must make the URL Rewriting.
I have GET variables passing in the URL and some have spaces that are encoded in the URL by "%20", for example:
mapage.php?produit=aménagements%20bois
So I apply my rewrite rule in the .htaccess file:
RewriteRule ^ma-page-amenagements-bois$ mapage.php?produit=aménagements%20bois [L]
The problem is that URL rewriting worked but a zero appears in my variable $ _GET instead of space ("aménagements0bois" instead of "aménagements bois") when I try the new URL, which distorts the dynamic display of my page.
I would like to know how to solve this problem.
Thank you
You don't need to add encoded characters in your rewrite rule, you can escape spaces with \:
RewriteRule ^ma-page-amenagements-bois$ mapage.php?produit=aménagements\ bois [L]
The reason you get a 0 in your url is because apache uses %1, %2, ... as rewrite variables. And because you don't have a %2, only the 0 remains.
I have been using '.htaccess' to rewrite how the page url should look so instead of:-
details.php?video=how+to+do+this&user=xxx
It should be more like this:-
/details/xxx/how+to+do+this
It's working and all, but here comes the issue; when I try to add a new $_GET category that wouldn't be useful all the time, that is the "page" get variable as not all video pages are going to have this variable. So when I add this variable nothing is set, it does show in the URL however.
/details/xxx/how+to+do+this?page=2
Here is the actual line of code that I used to rewrite one of the pages that's facing this issue.
RewriteRule ^user/(.*)/(.*)$ user.php?user=$1&view=$2
You can use:
RewriteRule ^user/(.*)/(.*)$ user.php?user=$1&view=$2 [NC,L,QSA]
QSA|qsappend When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query
string, and replace it with the newly generated one. Using the [QSA]
flag causes the query strings to be combined.
https://httpd.apache.org/docs/current/en/rewrite/flags.html
I need to grab some of my website's old URLs and do a 301 redirect to the new ones, since they are already indexed and we don't want to loose relevance after the change. The old URL is in fact very ugly and for some reason everything I try to do to rewrite it does not work. Here it is:
http://www.mywebsite.com/ExibeCurso.asp?Comando=TreinamentoGeral&codCurso=136&Titulo=Como%20Estruturar%20um%20Sistema%20Gerencial%20de%20Controles%20Organizacionais,13
Basically, I need to translate it into something like:
http://www.mywebsite.com/curso/136
From the old URL I need to check if the user typed "ExibeCurso.asp"; then I know I must send him here: /curso. I must also grab the integer that was in the querystring parameter "codCurso" (136). What is the regular expression I must use for this. I am using ISAPI_Rewrite 3, which basically implements htaccess on IIS, so there should be no difference in terms of syntax. Thanks.
Try this rule:
RewriteCond %{QUERY_STRING} ^([^&]*&)*codCurso=([0-9]+)(&.*)?$
RewriteRule ^/ExibeCurso\.asp$ /curso/%2? [L,R=301]
But I’m not sure whether ISAPI Rewrite requires the pattern to begin with a slash.
Off the top of my head, something like this should work:
RewriteRule ^ExibeCurso.asp(.*)$ http://www.mywebsite.com/curso/$1 [L,R=301]
That would at least send the traffic to /curso/ with all parameters attached. Maybe it's best to process it from there.
I would like to rewrite the following URL
www.mysite.com/mypage.php?userid=ca49b6ff-9e90-446e-8a92-38804f3405e7&roleid=037a0e55-d10e-4302-951e-a7864f5e563e
to
www.mysite.com/mypage/userid/ca49b6ff-9e90-446e-8a92-38804f3405e7/roleid/037a0e55-d10e-4302-951e-a7864f5e563e
The problem here is that the php file can be anything. Do i have to specify rules for each page on the .htaccess file?
how can i do this using the rewrite engine in php?
To get the rewrite rule to work, you have to add this to your apache configs (in the virtualhost block):
RewriteEngine On
RewriteRule ^([^/]*)/userid/([^/]*)/roleid/(.*)$ /$1.php?userid=$2&roleid=$3 [L,NS]
RewriteRule basically accepts two arguments. The first one is a regex describing what it should match. Here it is looking for the user requesting a url like /<mypage>/<pid>/roleid/<rid>. The second argument is where it should actually go on your server to do the request (in this case, it is your php file that is doing the request). It refers back to the groups in the regex using $1, $2, and $3.
RewriteEngine on
RewriteBase /
RewriteRule ^mypage\/userid\/(([a-z0-9]).+)\/roleid\/(([a-z0-9]).+)$ www.mysite.com/mypage.php?userid=$1&roleid=$2
No you don't need a separate rule for every php file, you can make the filename variable in your regex something like this:
RewriteRule ^(a-z0-9)/userid/([a-z0-9].+)/roleid/([a-z0-9].+)$ $1.php?userid=$2&roleid=$3
If you want to rewrite the latter URL that is entered in the browser TO the first format, you would want to use a .htaccess file.
However, if you want to produce the pretty URLs in PHP (e.g. for use in link tags), then you have two options.
First, you could simply build the URL directly (instead of converting) which in my opinion is preferred.
Second, you could rewrite the first (ugly) URL to the pretty latter URL. You would then need to use preg_replace() in PHP. See http://php.net/manual/en/function.preg-replace.php for more info. Basically, you would want to use something like
$rewrittenurl = preg_replace("#mysite\.com\/mypage.php?userid=(([a-z0-9\-]).+)\&roleid=(([a-z0-9\-]).+)$", "mysite.com/userid/$1/roleid/$2", $firsturl);
Good luck!
I'm struggling with a redirect problem. I need to redirect the following URL with mod_rewrite thorough .htaccess
http://www.mysite.com/somescript.php?&lang=php&var=1&var=2
to the following
http://www.mysite.com/somescript.php?lang=php&var=1&var=2
So, basically I just need to remove the
&
before
lang=php
However, the order is important. Sometimes
&lang=php
appears after other variables in the querystring. in this scenario I need the
&
to remain part of
&lang=php
Is this possible?
To summarise, if &lang=php appears at the beginning of the query string, remove the &. If &lang=php appears anywhere else in the query string, the & must remain.
Hope this is clear!
I would change the script myself but unfortunately I am not the developer, and he doesn't seem too helpful at the moment; this is a quick fix.
I would replace ?& with ?:
RewriteCond %{QUERY_STRING} ^\&(.*)$
RewriteRule ^somescript\.php$ /somescript.php?%1 [L,R=301]
why don't you match "?&" and replace it by "?" ?
Something like:
RewriteRule ^(.*)?&(.*) $1?$2 [L]
(not tested)
Because I think the combination "?&" is never valid...(?)