Apache Rewrite rule confusion - php

I'm trying to convert a simple url (below) in to a blog-style url, but not quite sure how to do it, all of my other rules are working fine, but I can't seem to figure this one out.
URL I want to convert: http://www.website.com/myblog.php?id=1&title=My+blog+title
URL I want it to create: http://www.website.com/1/my-blog-title
What should the rule be?
Any assistance appreciated :)

Try this
RewriteEngine on
RewriteBase /
RewriteRule ([0-9]+)/([^.]+) myblog.php?id=$1&title=$2

Try this in your .htaccess file:
RewriteEngine on
RewriteRule ^(\d+)/([^/]+)$ myblog.php?id=$1&title=$2
But here the hyphens are not replaced by plus signs.

in your .htaccess file,
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /myblog.php?id=$1 [L]
You don't (well shouldn't) need to pass the blog title to the blog file, only the ID. Hope this works

Related

Arrangement, Skipping and URL re=writing

I tried to re-write on my own but unable to do so, basically, I want to re-write php-based URL with .htaccess but also need to skip few parameters of php URL for example;
Original URL:
http://example.com/details.php?id=62?title=billions-s01e09-webrip-x264-fumettv
Required Format:
http://example.com/billions-s01e09-webrip-x264-fumettv-id62.html
Format Sequence:
MySiteURL/Title-of-Post-PostID.html
Looking for kind response, please guide me how can i make this possible through .htaccess.
waiting for kind response...
Good day
As you already know that you have to change the .htaccess file in your server. You can use regx to achieve your required url. Using the following code can help you.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-]+)-id([0-9]+).html?$ details.php?id=$2&title=$1 [QSA,L]
worked fine for me. Hope will work for you too.
To rewrite
http://example.com/foobar-id123.html
to
http://example.com/details.php?id=123?title=foobar
You can use the following rule :
RewriteEngine on
RewriteRule ^([^-]+)-id([0-9]+)\.html$ /details.php?id=$2?title=$1 [NC,L]

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

RewriteRule will not work in .htaccess file

I have found several questions on Stack that have show lots and lot of examples, but I have tried everyone in the book and this refuses to work.
I am trying to convert.
www.domain.com/title/index.php?id=70048305
to
www.domain.com/title/70048305
I want it to be converted both ways. When they type in with the query string and with the clean url.
What is wrong with my rewrite?
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^title/(.+)$ index.php?id=$1 [L,QSA]
I should note that I've placed this .htaccess file both on the root and in the /title/ folder.
Try this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-+/]+)$ index.php?id=$1
RewriteRule ^([a-zA-Z0-9-+/]+)/$ index.php?id=$1
Don't match ^title/(.+)$ you need to be matching only numbers like so ^title/([\d]+)/?
Otherwise,
www.domain.com/title/index.php?id=70048305
will match and be redirected to:
index.php?id=index.php?id=70048305

URL Rewriting in Apache Not Working For Me

I am using WAMP and has created a website in a 'http://localhost/snap2/html' folder. I am trying to execute following Rewrite rule but this is not working for me.
Server is giving me an error below:
The requested URL /snap2/html/browsed.html was not found on this server.
My .htaccess file is located in html folder and its structure is as below:
`RewriteEngine On
RewriteRule ^decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1`
Website is in a structure like 'www/snap2/html
Infact I am trying to rewrite following url
http://localhost/snap2/html/decision.php?PanelID=20
in to
http://localhost/snap2/html/decision/20
Also Options +FollowSymLinks gives me an error 500 therefore, I have commented it.
Any help would be pretty much appreciated.
Try this:
RewriteEngine On
RewriteRule RewriteRule ^snap2/html/decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1
But I guess this is not what you want. But maybe you will see where you wrong.
Symbol ^ in regular expression means that following string should be at the beginning of URL. so you have to include entire path, see htaccess RewriteEngine for more examples
Your write RewriteRule twice. Try
RewriteRule decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1 [L, QSA]
Or
RewriteRule snap2/html/decision/([0-9]+)$ /snap2/html/decision.php?PanelID=$1 [L, QSA]

Want to rewrite url using htaccess

I am new to htaccess and want to do following thing,
I want to rewrite url like,
http://example.com/test/test.php to http://example.com/test.html
and my htaccess file present in test folder. Can anyone help to find out this problem.
Thank You
That should do it:
RewriteEngine on
RewriteRule ^(.*).html$ test/$1.php [QSA]

Categories