Use text from URL after slash as parameter - htaccess - php

I need to rewrite this:
http://example.com/new/location/
to:
http://example.com/new.php?p=location
Btw it could be anything after new/ for example i need:
http://example.com/new/anything
to actually be:
http://example.com/new.php?p=anything
But i need to keep the URL structure with the slashes for SEO and security purposes
How could i achieve this using .htaccess?
currently in my htaccess ive got:
RewriteEngine on
RewriteBase /dir/
Thanks

Add the following directive into your .htaccess:
RewriteRule ^new/location$ new.php?p=location [L]
or to work any values use:
RewriteRule ^new/(.*)$ new.php?p=$1 [L]

Related

How to rewrite specific URL using htaccess

I've been breaking my head on this for quite some time and I don't see the solution.
I want to rewrite a URL with a GET language parameter to a more clean URL.
For instance:
http://www.example.com?lang=en
Needs to be:
http://www.example.com/en
The above works fine with this rewrite rule:
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L]
But I can't get it to work on URLs like these:
http://www.example.com/contact.php?lang=en
http://www.example.com/about.php?lang=en
That need to be:
http://www.example.com/en/contact.php
http://www.example.com/en/about.php
Anyone have an idea what I'm missing in my rewrite rule to make this work?
You will need an additional rewrite rule for handling /en/about.php:
RewriteEngine On
RewriteRule ^(en|nl|fr|de)/([\w-]+\.php)$ $2?lang=$1 [L,QSA]
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L,QSA]

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

Search and replace part of URL via apache htaccess RewriteRule

I'd like to rewrite URLS like:
http://post.local/web/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en
to
http://post.local/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en
Means the part "web/" should be replaced by ""(or remove it), but I have problems to build a rewrite rule for my .htaccess which cover that.
Note: mod_rewrite is on (RewriteEngine on)
RewriteRule ^web/bundles/(.*) bundles/$1 [QSA]
This should work:
RewriteEngine On
RewriteRule ^web/bundles/(.*) /bundles/$1 [R]
The above will remove web/ from the url so
http://post.local/web/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en
will become
http://post.local/bundles/silverkixcms/elFinder/elfinder.html?CKEditor=silverkix_cmsbundle_pagetype_content&CKEditorFuncNum=1&langCode=en

htaccess Rewrite for clean URLs 2 get variables

Url rewriting doesn't seem to work.
I want to rewrite http://www.domain.com/files.php?key=file&id=10 to file/10
So this is the code I wrote in my .htaccess file:
RewriteEngine On
ReWriteRule ^(.*?) files.php?key=$1&id=$2
Doesn't seem to work. Anyone having a idea why?
You need two groups to use $2. Try
RewriteEngine On
ReWriteRule ^([^/]+)/(\d+)/? files.php?key=$1&id=$2
[^/]+ Means one or more symbols each of them isn't slash
Try:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z]+)/([0-9]+)/$ files.php?key=$1&id=$2

.htaccess redirect normal url to query string

I have a url of the form:
www.example.com/r/abc/1234
that I want to rewrite to:
www.example.com/r.php?deb=abc&id=1234
And another url:
www.example.com/r/12bcd-qsqs-343-wdwd/1234
which should be rewritten to:
www.example.com/r.php?abc_id=12bcd-qsqs-343-wdwd&id=1234
Both of these rule should be handled by .htaccess. Any suggestions?
Both the URIs that you want to redirect look similar except that 2nd one has hyphens - in first query parameter.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^r/([^-]+)/(.+)/?$ r.php?deb=$1&id=$2 [L,QSA]
RewriteRule ^r/([a-z0-9-]+)/(.+)/?$ r.php?abc_id=$1&id=$2 [L,QSA,NC]
Try something like :
RewriteRule (.*)/(.*)/(.*)$ path_to_php_files/$1.php?deb=$2&id=$3 [QSA,L,NC]
Unfortunately with this rule, you would have the first $_GET parameter always deb and I hope you have the DirectoryIndex set up corectly. I do not think that for
www.example.com/r/abc/1234
www.example.com/r/12bcd-qsqs-343-wdwd/1234
you can delivrer the abc and 12bcd-qsqs-343-wdwd to 2 different $_GET parameters if you have dynamic links.

Categories