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.
Related
Using WamServer , I used Generate It! to over write a URL like
The original URL:
http://localhost/CMS/foo.php?id=2&slug=eyeglasses
The rewritten URL:
http://localhost/2/eyeglasses.php
The rule which site gave me is
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /CMS/foo.php?id=$1&slug=$2 [L]
but this is still returning original URL
http://localhost/CMS/foo.php?id=2&slug=eyeglasses
Can you please let me know what I am doing wrong?
Update
To be more clarify! I have an index.php which contains a link like
echo ''.$theName.'';
now we user landed in foo.php the URL looks like
http://localhost/CMS/foo.php?id=1&slug=eyeglassess
correct? what I would like to do is displaying the
http://localhost/2/eyeglasses.php
instead of original URL
You need a redirect rule to redirect old URL to pretty URL. This code should be in /CMS/.htaccess
RewriteEngine On
RewriteBase /CMS/
# redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /CMS/+foo\.php\?id=([^\s&]+)&slug=([^\s&]+) [NC]
RewriteRule ^ %1/%2.php? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)\.php$ foo.php?id=$1&slug=$2 [L,NC,QSA]
However it is better to change your link code to new pretty link scheme.
.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 on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.
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]
I want to apologize for not being a strong knowledge of the language, but I have a problem like that.
I made a redirect through mode_rewrite now url is "localhost/url/url2/" apache and it redirects to
"localhost/url/url2/index.php" how to remove from url "index.php?"
if you want to remove only index.php then put code like this in htaccess,
RewriteRule ^/$ http://localhost/url/url2/index.php
place this htaccess file where index.php is
You can use this rule:
RewriteRule ^url/url2/$ /url/url2/index.php [L]
Browser won't show index.php in the URL input.