I am not very good with .htaccess, and I am trying to rewrite URLs with variables. I tried to follow other topics advice but couldn't get it to work.
I go some urls that are like this:
/en/22-products/ (the url continue but I don't want to change the rest)
I would like to change them like this: /en/catalog/22-products/
Now I got this in my .htaccess:
RewriteRule ^en/([0-9]+)-products/?$ /en/catalog/$1-products/ [R,L]
But it won't work. Where am I wrong?
You were not far. We also need to pick up the trailing part of the url. Do it like this:
RewriteEngine On
RewriteRule ^en/([0-9]+-products)(.*) /en/catalog/$1$2 [L,R]
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 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]
I have bunch of URLs
http://www.mydomain.com/aboutUs.php
http://www.mydomain.com/contactUs.php
and so on....
The Rewrite Rule I wrote in my .htaccess file is
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)$ contactUs.php
RewriteRule ^([a-zA-Z0-9]+)$ aboutUs.php
and so on...
Now when I use the clean URL, let say for example,
http://www.mydomain.com/aboutUs
It points to homepage always...i.e. http://www.mydomain.com/, BUT the URL remains http://www.mydomain.com/aboutUs
And similar behaviour for other URLs as well.
Also the images in the banner doesnt appear while using clean URL... As I have used a src attribute for images as <?php echo HTTP_PATH;?>images/header-logo.png
Where, HTTP_PATH = "http://" . $_SERVER["HTTP_HOST"])
Could someone guide me whats wrong with the .htaccess or is it anything else?
UPDATE as per #HAKRE Comment
I need the URL to look like http://www.mydomain.com/aboutUs and it should point to http://www.mydomain.com/aboutUs.php
Try and use a redirect; put a [R] after the rewrite statement, or [R,L] if you want apache to stop there. Or you can use [R=301] format eg to send a 301 permanent redirect code as well.
So a row looks like this:
RewriteRule ^([a-zA-Z0-9]+)$ contactUs.php [R]
hope this helps.
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]
alright... so I have this css bundler, using the following link:
http://www.example.com/min/?b=wp-content/themes/mytheme&f=style.css,boxes.css,mods.css,scripts.css
Parameter b is the css folder url and parameter f is the css files to get.
However, to help out my cache, I want the question mark gone. If possible, something like:
http://www.example.com/min/b=wp-content/themes/mytheme&f=style.css,boxes.css,mods.css,scripts.css
I tried the following:
RewriteEngine On
RewriteRule ^/([^/\.]+)/?$ index.php?b=$1 [L]
Needless to say, It did not work.
Your rules are going to loop, you can try adding a condition in front of your rule so that it looks like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^/([^/\.]+)/?$ index.php?b=$1 [L]
But it seems, based on your example URLs, you want something more like this:
RewriteRule ^min/b=(.*)$ index.php?b=$1 [L]
This looks like an unusual thing to do where you want to get rid of ? from query string but want to keep ampersand i.e. &.
Anyway if that's what you really want then you can put this code in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^min/b=([^;]*);f=(.*)$ min/?b=$1&f=$2 [L]
This rule will internally redirect http://www.example.com/min/b=wp-content/themes/mytheme;f=style.css,boxes.css,mods.css,scripts.css to http://www.example.com/min/?b=wp-content/themes/mytheme&f=style.css,boxes.css,mods.css,scripts.css