simple htaccess RewriteRule to map 404 URL - php

I have this URL:
mysite.com/index.php?pageType=404
and then I want it to get mapped to:
mysite.com/404
I have written the following rewrite rule but it does not work, what is the problem?
RewriteEngine on
RewriteRule mysite.com/404 mysite.com/index.php?pageType=404
IndexIgnore *
Thanks in advance

RewriteRule's first argument is a regex that matches everything behind the domain name and before the query string. For an url like http://example.com/my/site/is/awesome?give=mecookies, it would match my/site/is/awesome (without the first slash).
The correct rule would be:
RewriteRule ^404$ index.php?pageType=404 [L]
Please read the documentation for more information about this.

Related

mod_rewrite seo friendly url passed as parameter only works with prefix on url

I'm successfully using mod_rewrite to grab the url and pass it as a parameter but only when the url has a prefix of at least one character.
The .htaccess file is in the directory www.site.com/content
For example, the following Rewrite succeeds: (site.com/content/-seo-friendly-url)
RewriteEngine on
RewriteRule ^content-(.*)$ ./get_content.php?url=$1
but this example does not work: (site.com/content/seo-friendly-url)
RewriteEngine on
RewriteRule ^(.*)$ ./get_content.php?url=$1
I get a 500 Internal Server Error
and this gets a 404 Not Found
RewriteRule ^([a-zA-Z0-9-])$ ./get_content.php?url=$1
and this passes 'get_content' as the ($1) parameter to get_content.php with no error
RewriteRule ^(.*)$ get_content.php?url=$1
The goal is to pass the url to get_content.php without a prefix.
For example,
www.site.com/content/seo-friendly-url
should be rewritten to
www.site.com/content/get_content.php?url=seo-friendly-url
try this. I haven't tested this code, but I'm pretty sure it may help you
RewriteEngine On
RewriteRule ^content/get_content.php\?url\=seo-friendly-url$ /content/seo-friendly-url [L]
If this didn't work i recommend to use this site. It helped me with a lot of rewrite's
Click this link!
Try this rule,
RewriteRule ^([\w-]+)$ get_content.php?url=$1

.htaccess URL rewrite ignored

.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.

Rewriting url having more than one url parameter

Has been researching on how to rewrite url using htaccess file. After much time invested, I still have very vague idea of how a url will be rewritten.
Basically, I have a url:
http://mehthesheep.com/bali/jimbaran/blue-point/1
I would like to rewrite to:
http://mehthesheep.com/countries/hotel.php?hotel_id=1&state=bali&city=jimbaran
How do I go about having more than one parameter in the url?
Any help with some simple explanation would be appreciated!
So apache isn't my strongest skill, but from what I understand the way you would rewrite the urls is with mod rewrite, and in your regex, each matching parameter can be mapped to something else. So something like:
#1st field #match 2
RewriteRule ^/hotel/(.*)/(.*) http://www.example.com/hotel.php?hotel_id=$2&state=$1 [NC,QSA,L]
#2nd field #match 1
My syntax might be wrong so I apologize, but the gist is that I can take patterns and then correspond the matches to '$n' keys in my rewrite url. In the above example a call to /hotel/bali/1 would rewrite to hotel.php?hotel_id=1&state=bali. Not sure if it matters, but I used a very simple match '(.*)' basically means anything.
The last bit w/ are flags you can add to make sure you get the desired behavior. QSA allows for a querystring to be attached in case you had tracking or other params come in, NC makes the matching case insensitive, in the event they try something like /Hotel. L is for last, meaning if the rule is matched this is the last rule to evaluate so execute the rewrite (or something to that effect).
Hope that helps. This really helped me dig into mod rewrite:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /countries/hotel.php?hotel_id=$4&state=$1&city=$2 [L,QSA]

modify URL using .htaccess

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.......

URL Mod-Rewrite

I currently have a working URL:
http://example.com/security-services.php?service=fixed-camera-surveillance
and then I have PHP say, $_REQUEST['service'] to do some stuff...
But I'd like to achieve the same function if the URL looked like this:
http://example.com/security-services/fixed-camera-surveillance
Thanks!
An .htaccess file with something like this should do it.
Options +FollowSymLinks
RewriteEngine On
RewriteRule security-services/(.*)/? security-services.php?service=$1 [L]
The part that says security-services/(.*)/? matches the URL in the browser and rewrites it to security-services.php.
The key part is the (.*) which captures that portion of the URL and passes it to the PHP script as a GET value.
Try this rule:
RewriteEngine on
RewriteRule ^security-services/([^/]+)$ security-services.php?service=$1 [L]
The [^/]+ describes the next path segment after /security-services, (…) forms a group that’s match then can referenced to with $1 in the substitution.
And if you want a more general for any kind of …-service.php file:
RewriteEngine on
RewriteRule ^([^/-]+)-services/([^/]+)$ $1-services.php?service=$2 [L]
You can create a rule like this:
RewriteRule ^security-services/(.*)/? /security-services.php?service=$1
If you're using a .conf file, change ^ to ^/

Categories