I'm looking for a way to rewrite my URLs and am checking what htaccess can do for me.
I want to rewrite
domain.com/index.php?page=myPage&name=test
to
domain.com/myPage/?name=test
All tutorials I've found only shows how to do complex things but something as simple as this, isn't to be found anywhere.
What I have only rewrites "myPage" to "page=myPage" and ignores what I put after "?" in my URL
RewriteRule ^([^/\.]+)?$ index.php?page=$1 [L]
Notice that "name=test" could be anything (id=1, mode=edit, foo=bar).
Try adding the flag for "query string append" by changing [L] to [QSA,L]
Source: https://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa
Related
I've been breaking my head on this for quite some time and I don't see the solution.
I want to rewrite a URL with a GET language parameter to a more clean URL.
For instance:
http://www.example.com?lang=en
Needs to be:
http://www.example.com/en
The above works fine with this rewrite rule:
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L]
But I can't get it to work on URLs like these:
http://www.example.com/contact.php?lang=en
http://www.example.com/about.php?lang=en
That need to be:
http://www.example.com/en/contact.php
http://www.example.com/en/about.php
Anyone have an idea what I'm missing in my rewrite rule to make this work?
You will need an additional rewrite rule for handling /en/about.php:
RewriteEngine On
RewriteRule ^(en|nl|fr|de)/([\w-]+\.php)$ $2?lang=$1 [L,QSA]
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L,QSA]
.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'd like to rewrite URLS like:
http://post.local/web/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en
to
http://post.local/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en
Means the part "web/" should be replaced by ""(or remove it), but I have problems to build a rewrite rule for my .htaccess which cover that.
Note: mod_rewrite is on (RewriteEngine on)
RewriteRule ^web/bundles/(.*) bundles/$1 [QSA]
This should work:
RewriteEngine On
RewriteRule ^web/bundles/(.*) /bundles/$1 [R]
The above will remove web/ from the url so
http://post.local/web/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en
will become
http://post.local/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en
What is wrong in this rule?
RewriteRule ^page\?v=([^/]+)$ page.php?v=$1 [L,NC]
I just want to make the URL looks like that
http://www.domainname.com/page?sk=info
You don't have to include the query parts if they're not changing anyway.
RewriteRule ^page$ page.php [L,NC]
The RewriteRule will not match any part of the query string. page?v=123 will still become page.php?v=123
Also, your RewriteRule uses ?v= while you talk about ?sk=info
Also, you can find additional information about this mod_rewrite case here:
http://wiki.apache.org/httpd/RewriteFlags/QSA
And another SO entry about this issue here.
I can’t seem to make this work - and I’m pretty sure that the real problem is that I just starring myself blind on it, so I hope a pair of fresh eyes can help me out.
What I wan’t to do is have several applications attached to my system.
At this time, a website already exists in the root folder, but I wan’t some microsites/powerformats in a CI installation.
My mod_rewrite looks like this:
RewriteCond %{REQUEST_URI} ^/(powerformat1|powerformat2)/?$
RewriteRule ^(.*)$ powerformats/index.php/$1 [L]
Although, getting the CI index.php correctly, when trying to access example.org/powerformat1 or example.org/powerformat2 gives me CI’s 404 page.
It seems like whatever I try of rewrite rules I either get the 404 page or nothing at all.
Any insights?
-- EDIT --
What I believe is my problem is that CI actually gets the 'powerformat1' string passed as the first segment. That is what I need to avoid. But can't that be solved through mod_rewrite?
You could try link directly to the file with the appropriate query string instead
RewriteRule ^(.*)$ /powerformats/index.php?somequery=$1 [L]
(you may have to change the slashes, see below)
Or it may be this:
Accessing /powerformat1/
may be rewriting to
powerformats/index.php//powerformat1/
You could try
RewriteRule ^/(.*)/$ /powerformats/index.php/$1 [L]
or some other variation with slashes:
RewriteRule ^/(.*)/$ /powerformats/index.php/$1/ [L]
Did you miss RewriteEngine On ?