I want to change url for example http://www.mysite.com/cgshop/admin/index.php?route=common/home will become http://www.mysite.com/cgshop/cp/index.php?route=common/home.
Please help me to figure out this. Thanks in advance.
This should get you what you want.
It tests to see if the URI begins with /cgshop/admin/ and then captures the rest so it can redirect with a 301 to the new url. The QSA means it will also carry over the query string (everything after the ?).
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cgshop/admin/.*$
RewriteRule ^cgshop/admin/(.*)$ /cgshop/cp/$1 [R=301,L,QSA]
RewriteEngine On
RewriteRule ^cgshop/cp/.*$ cgshop/admin/$1
Try this.......
Related
I have my site url like this.
https://example.com/page/1/?orderby=low_to_high&s=demo+post&post_type=post&twdg_wsac=1
I want to replace some part of this url using htaccess so the final output should be like this
https://example.com/page/1/?orderby=low_to_high&s=demo+post&post_type=post&custom_posts=1
So I have made changes in the htaccess file like this
RewriteEngine on
RewriteRule ^&twdg_wsac$ &custom_posts
But its not working. Also as you can see in the url there is twdg_wsac=1. So the last "1" is a pagination for post so that would change dynamically as per posts count.
So can someone tell me how to do this?
Any help and suggestions would be really helpful. Thanks
Update
All the parameters in the url are dynamically generated I mean those are the filter parameters. So they cant be same except twdg_wsac
Try with below, we are doing rewrite on query string.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^orderby=(.+)&s=(.+)&post_type=(.+)&twdg_wsac=([\d]+)$
RewriteRule ^ %{REQUEST_URI}?orderby=%1&s=%2&post_type=%3&custom_posts=%4
RewriteEngine On
RewriteCond %{QUERY_STRING} twdg_wsac=([\d]+)$
RewriteRule ^ %{REQUEST_URI}?orderby=low_to_high&s=demo+post&post_type=post&custom_posts=%1 [L]
.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'm editing a .htaccess file for redirection from www.oldwebsite.com to www.newwebsite.com + specific redirection for pages.
the following code is working good :
RedirectPermanent /page.html http://www.newwebsite.com/newpage.html
Where I got problem is when I try to redirect pages ending like this with redirectpermanent :
oldpage.php?id=1
At this point I get a 404 error back.
I tried another solution that is this code
RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^oldpage\.php$ http://www.newwebsite.com/newpage2.html [R=301,L]
This is working excepted the browser makes me go to the following link :
http://www.newwebsite.com/newpage2.html?id=1
Can someone help me with this issue. I would like to use Redirect permanent (doesnt work, same thing with Redirect seeother). I think solution is easy but I don't get a detail I think.
Thanks !!!
just add ? at the end of rewriteRule to override query string
RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^oldpage\.php$ http://www.newwebsite.com/newpage2.html? [R=301,L]
You can use PHP $_SERVER['PHP_SELF'] method for this..
so,
$page = $_SERVER['PHP_SELF'];
header("Location: http://www.newwebsite.com$page");
It will redirect http://www.oldwebsite.com/newpage.html to http://www.newwebsite.com/newpage.html
or instead of $_SERVER['PHP_SELF'] you can try $_SERVER['REQUEST_URL']
If you are trying to drop the query string, you can use the QSD flag.
RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^oldpage\.php$ http://www.newwebsite.com/newpage2.html? [R=301,L, QSD]
the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies
I'm making a redirect script for my site, I use htaccess to rewrite the URL so it looks nicer.
eg. http://localhost/r/http://google.com is the URL, but when I printing the value it shows up like this http:/google.com.
One / is missing, how can I fix that?
Edit:
Rewrite rule:
RewriteRule ^r/(.*)/$ /system/offsite/redirect/index.php?url=$1 [L]
Thanks for any help :)
This behavior is due to Apache that removes empty path segments before mapping it. But you can access the original requested URI path via THE_REQUEST:
RewriteCond %{THE_REQUEST} ^GET\ /r/([^\ ]+)
RewriteRule ^r/ /system/offsite/redirect/index.php?url=%1 [L]
Use php urlencode function
EDIT:
//echo your url
echo 'http://localhost/r/'. urlencode('http://google.com');
and in your index.php file
//get your url
$url = urldecode($GET['url']);
I think REQUEST_URI variable will have correct text. Use it like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/r/(.*)$
RewriteRule ^r/ /system/offsite/redirect/index.php?url=%1 [L,QSA,NC]