I have several URLs which are dealt with by rewrite rules. Basically I want to add some $_GET-vars to a rewritten URL.
Say I have:
http://www.domain.com/search/the-netherlands/overijssel/
I would like to add some extra data to the URL like:
http://www.domain.com/search/the-netherlands/overijssel/?custom_var=1
My RewriteRule looks like this:
RewriteEngine On
RewriteRule ^search/(.*)/(.*)/$ /index.php?page=Search&Country=$1&Region=$2 [L]
When I var_dump the contents of $_GET I don't see custom_var anywhere. Can this be done, using the existing rewrite rule?
Since your query already has some parameters, append [QSA] to the end of the RewriteRule.
RewriteRule ^search/(.*)/(.*)/$ /index.php?page=Search&Country=$1&Region=$2 [QSA,L]
Related
.htaccess newbie here.
I have a URL like this:
example.com/lesson-plans/earth-sciences/show_lesson_plan.php?title=Some_Title&id=6
that I need to be rewritten like this:
example.com/lesson-plans/earth-sciences/some-title-6
I am using the following .htaccess URL rewrite:
RewriteEngine On
RewriteRule ^lesson-plans/earth-sciences/([^/]*)/([^/]*)$ /lesson-plans/earth-sciences/show_lesson_plan.php?title=$1&id=$2&cat=3 [L]
However, when I hover over/click on links of the original format (example.com/lesson-plans/earth-sciences/show_lesson_plan.php?title=Some_Title&id=6), they are not being rewritten. I've tried a few different rewrite rules, but nothing works.
How can I make this rewrite work? As far as I know, .htaccess is working on my server and rewrites are permitted.
You were close
RewriteEngine on
RewriteCond %{QUERY_STRING} title=([^&]+)&id=([^&]+)
RewriteRule ^lesson-plans/earth-sciences/show_lesson_plan.php$ /lesson-plans/earth-sciences/%1-%2 [QSA,L,R]
Randomly while lying in bed last night.
You have the rewrite rule back to front. you have to add the rule for the rewritten url to turn it back into an ugly one
see: http://martinmelin.se/rewrite-rule-tester/
RewriteEngine On
RewriteRule ^lesson-plans/earth-sciences/(.*)-(.+)$ /lesson-plans/earth-sciences/show_lesson_plan.php?title=$1&id=$2&cat=3 [L]
so
lesson-plans/earth-sciences/some-title-6
becomes
/lesson-plans/earth-sciences/show_lesson_plan.php?title=some-title&id=6&cat=3
I ended up using the following code and am posting it as an answer in the event someone else finds this useful:
RewriteEngine on
RewriteRule ^([^/.]+)/([^/.]+)$
show_lesson_plan.php?title=$1&id=$2
This will take a url (like this: http://example.com/lesson-plans/earth-sciences/a-cloud-in-a-bottle-i/8923) and run it through this: http://example.com/lesson-plans/show_lesson_plan.php?title=$1&id=$2
Note that I changed the original URL slightly, opting to break the id out of the title string.
I have a single get data (affid) that i want to retrieve using .htaccess but i don't know how to do it. Can you help me out?
Here is an example of links:
mysite.com/searchmembers?affid=1001
mysite.com/profle/123?affid=1002
mysite.com/videos/567?affid=1003
Another thing that might give a problem is these links already have been rewritten on .htaccess
RewriteRule ^searchmembers? index.php?task=searchMembers
RewriteRule ^profile/(.*)? index.php?task=viewProfile&id=$1
RewriteRule ^videos? index.php?task=videos&id=$1
i just want to retrieve the affid and add it to the links like this:
RewriteRule ^searchmembers... index.php?task=searchMembers&affid=(data retrieved)
RewriteRule ^profile/(.*)... index.php?task=viewProfile&id=$1&affid=(data retrieved)
RewriteRule ^videos? index.php... task=videos&id=$1&affid=(data retrieved)
i know i could add it on htaccess for each of these links but if there is an easier way to do this then it would be a great help. thank you for any response that i will receive!
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^searchmembers$ index.php?task=searchMembers [QSA,NC,L]
RewriteRule ^profile/(.*)$ index.php?task=viewProfile&id=$1 [QSA,NC,L]
RewriteRule ^videos/(.*)$ index.php?task=videos&id=$1 [QSA,NC,L]
If you just need to append a new query parameter (like task here), any existing query parameters can be appended automatically using the [QSA] (Query String Append) flag. I've also corrected the regex used in your RewriteRules. The use of ? is incorrect.
I'm trying to write content of .htaccess such a way that do the following conversion:
www.abc.com/page.php?id=1 to www.abc.com/page/1
So I tried to use the following RewriteRule.
RewriteRule ^page/([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
I'm wondering, If page.php needs more that 1 PHP request, for instance, id and pagenum or even 3 or more, what is the best way to implement this (while using BeautifulURL)?
I tried to use www.abc.com/page/1?pagenum=5 but it does NOT working, as you know.
With a URL like: www.abc.com/page/1?pagenum=5, you need the QSA flag:
RewriteRule ^page/([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L,QSA]
or else the pagenum=5 won't get added. Additionally, you can have the URL look (optionally) like this:
http://www.abc.com/page/1/5
for: /page.php?id=1&pagenum=5 using this rule:
RewriteRule ^page/([A-Za-z0-9-]+)/([0-9]+)/?$ page.php?id=$1&pagenum=$2 [L,NC]
You'd need both rules if you want both to work, with the extra /5 at the end and without it.
I'm interested in rewriting a url which is already "friendly" in order to clean up what is already in the path.
At the moment, I have a URI which points to http://example.com/page/index/home, but ideally id like to have the remove page/index completely, and just have http://example.com/home.
Something like the following...
RewriteEngine On
RewriteRule ^page/index/home$ /home [L]
If you know the specific page you are matching
To achieve what you want, you can just do this:
RewriteEngine On
RewriteRule ^/home$ /index.php [QSA,L]
QSA stands for Query String Append. It will append the query string (your GET parameters) to the end of your URL automatically if there is one.
You can use this rule in .htaccess file:
RewriteEngine On
RewriteRule ^home$ page/index/home [NC]
I have a URL like index.php?url_id=u5531e3aas01fe5c2 and I also want to make it work like /u5531e3aas01fe5c2, that it to pass a parameter to my PHP, but something is wrong with my code.
RewriteEngine On
RewriteBase /try/
RewriteRule /u[a-z0-9]{17} /index.php?url_id=$1 [L,QSA]
P.S.
And also is it possible to rewrite URL that if appears index.php?url_id=u5531e3aas01fe5c2, it would be rewritten to /u5531e3aas01fe5c2
Try this:
RewriteRule ^u([a-z0-9]{16}) /index.php?url_id=$1 [L,QSA]