Deleting all php extension and get rid of variables in the url - php

I got this url
/localhost/andalucia/productdetails.php?value=20
I want to change it to
/localhost/andalucia/productdetails/20
how do you do this and how will you get the value 20 in the handler page?
should I change the coding or I just add an ht access file?
If it is adding a ht access file what code should be in it?
How if I have more pages like:
/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus
Will they be affected automatically too?
ok i tried to use
RewriteRule ^productdetails/([0-9]+)$ productdetails.php?value=$1 [L,QSA]
but now the problem is all my pictures is gone in the html and i cant open the other page like
/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus
i need a htaccess code that can open all of these
/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus
/localhost/andalucia/productdetails/20
pls helpp someone

With the apache extension mod_rewrite it is really easy to transform your pretty URLs into the URLs needed by your script. This .htaccess example which you place in your web root should get you going:
RewriteEngine On
RewriteRule ^andalucia/productdetails/([0-9]+)$ /andalucia/productdetails.php?value=$1 [L]
This example will only rewrite andalucia/productdetails/NNNN... format URLs, all other URLs won't be affected. If you need to pass other query parameters, like /andalucia/productdetails/20?sort=asc you need to pass the QSA flag (query string append) to the rewrite rule:
RewriteRule ^andalucia/productdetails/([0-9]+)$ /andalucia/productdetails.php?value=$1 [QSA,L]
The L flag will prohibit the evaluation of next rules. Just look up the mod_rewrite documentation for a in-depth discussion!

Related

htaccess rewrite rule for url varible as parent folder

I already create a .htaccess in my folder and would like to make the URL:
www.example/good/page
toward:
www.example/page?type=good
my current document is written as:
RewriteRule ^(\w+)$\/page page.php?type=$1 [NC,L]
but it didn't work, and I don't know how to check it is correct or not
would anyone able to provide some example for that?
thanks!
Make sure your rewite engine is on, and it is rewriting the base. Change your code like the following:
RewriteEngine on
RewriteBase /
RewriteRule ^/?([^/]+)/page?$ "page.php?type=$1" [L,QSA]
And then your rewrite rule must look like this.
The RewriteRule basically means that if the request is done that matches ^/?([^/]+)/page?$ (matches any URL except the server root), it will be rewritten as page.php?type=$1 which means a request for page.php be rewritten as page.php?type=good).
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (example.com/good/page?id=2 will be rewritten as page.php?type=good&id=2.
L means if the rule matches, don't process any more RewriteRules below this one.
Try this and let me know. Accept the answer if it worked for you.

changing php function calling URL with mod_rewrite

I've already search for an answer here, but couldn't find an exact answer.
I've got a php application (it uses CodeIgniter) which is connected to our companys Management Database. The application provides information out of the database in xml form so that our internal mediawiki's can receive those and build Info-Boxes (as example) out of them.
I have the following link to my data in xml format:
https://example.com/controller/function/databaseID/short_name or
https://example.com/App/makeInfoBox/258/Applicationname
which contains Infromation as following:
-<infobox>
<id>258</id>
<short_name>Applicationname</short_name>
<long_name>Long Applicationname</long_name>
<app_number>334</app_number>
<status>End of life</status>
...
</infobox>`
I now want mod_rewrite to change the url to:
https://example.com/appInfoBox.xml?id=258&short_name=Applicationname
I've got something like this:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)\&short_name=(.*)$
RewriteRule ^appInfoBox\.xml$ App/makeInfoBox/%1/%2
I'm really not good in mod_rewrite and this code I got is based on a older version of a used .htaccess file.
Any Suggestions? Thanks!
You must capture the relevant parts of the request (...) and use this in the substitution $...
RewriteRule ^App/makeInfoBox/(.+?)/(.+)$ /appInfoBox.xml?id=$1&short_name=$2 [L]
Use the following directives in the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/App/makeInfoBox/([0-9]+)/(.*)$
RewriteRule ^(.*)$ /appInfoBox.xml?id=%1&short_name=%2 [QSA,R=302]
The .htaccess file should be present in the root folder of example.com.
The RewriteCond directive will check whether the request matches the pattern /App/makeInfoBox/(any-number)/(any-number-of-characters).
IF this is true the RewriteRule directive will map the request to example.com/appInfoBox.xml?id=(any-number)&short_name=(any-number-of-characters) using the QSA flag.
The R=302 flag will cause an external redirect and the end user will be able to see the new URL with query string on the browser.
If you don't want to display the URL with query string to end users, just remove the R=302 flag.
Using your example, now if you will access https://example.com/App/makeInfoBox/258/Applicationname it will redirect to https://example.com/appInfoBox.xml?id=258&short_name=Applicationname

How to get Dynamic URL With help of .htaccess and PHP for Good SEO

For example i have
3 php pages
www.example.com/page1.php?var1=data1&var2=data2&var3=data3
www.example.com/page2.php?var1=data1&var2=data2&var3=data3
www.example.com/page3.php?var1=data1&var2=data2&var3=data3
For good SEO . I need URL like
www.example.com/page1/data1-data2-data3
www.example.com/page2/data1-data2-data3
www.example.com/page3/data1-data2-data3
I got something URL rewriting with PHP but am confused how to implement it for multiple dynamic PHP pages.
i need all the variables for proper functioning of php pages
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(page1|page2|page3)/([^-]+)-([^-]+)-([^-/]+)/?$ /$1.php?var1=$1&var2=$2&var3=$3 [L,QSA,NE]
In your .htaccess you should add the line:
RewriteRule ^(\w+)/(\w+)-(\w+)-(\w+)$ /$1.php?var1=$2&var2=$3&var3=$4 [QSA]
The first part captures the page name (script name, in your case), then each successive query string parameter. The QSA key tells the redirect to take any additional query string parameters not specified explicitly along with the request.

url rewrite with string in .htaccess

I am trying to use mod_rewrite to redirect users keeping the multiple query string and creating a redirect page
For Example,
If user opens
http://localhost/url/url/http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
then he is taken to
http://localhost/url/url.php?redirect=http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
There is secondary problem that URL should be encoded and then redirected! If URL is not encoded then the string (&stackoverflow=great)would be not a part of 'redirect' string of url.php
I tried many solutions then came for stackoverflow! I tried the following code in following file
http://localhost/url/.htaccess
RewriteRule ^url/([^/])$ url.php?redirect=$1 [QSA,L]
but the result is localhost/url/url.php?redirect=http only
Your setup won't work with the unencoded inner url, so an 'answer' can only have temporary character. But this might be a starting point:
RewriteEngine on
RewriteRule ^/url/url/(.*)$ /url/url.php?redirect=$1 [L,QSA]
I wonder however if that fragment /url/url is really intended (the two 'url's in there).
Note that the exact rule content also depends on where you want to define that rule. The syntax is different whether you use the central server configuration (referred) or .htaccess style files (as second choice and more complex).
Try this
RewriteEngine on
Redirect ^url/url/(.*)$ url/url.php?redirect=$1
The basic redirect systax,
redirect accessed-file URL-to-go-to

mod_rewrite url

I noticed on youtube their url does not have a file extension and querystring. I've been trying to emulate something similar but found I had to either include the file extension or a trailing slash.
members.php?agefrom=20&ageto=40&city=london (works)
members/?agefrom=20&ageto=40&city=london (works)
members?agefrom=20&ageto=40&city=london (doesnt work)
So I was just wondering how can I get the third case to work? i've tried a few things in the htaccess file.
RewriteRule ^members$ index.php?page=members&%{QUERY_STRING} [QSA,L]
I have tried the above but to no avail.
Any help would be appreciated.
The RewriteRule that you posted is correct for members.php? and for members? It should not work with members/
You must have additional RewriteRules before this one that are getting applied first and are affecting this rule.
However, here is a rule that should still work for you:
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
The /? is saying to match if the slash exists or if it doesn't exist.
Have you tried to remove the $ on the end?
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
This did work in the end, all I had to do was move it nearer the top of the htaccess file. I had the following line which I guess was being read instead.
....
RewriteCond %{REQUEST_URI} ^/members$ [OR]
....
I am changing my approach to SEO URL's because I was trying to find articles on how the googlebot actually crawls forms and how it prefers the GET method. I was using jquery to alter my action parameter to write the following URL:
/members/london/18-to-25
I dont know how much google likes jquery and whether it would scan javascript code. I am assuimg it just follows the HTML code and having done some research I have changed my form to use the GET method and so the bot can crawl my form without complaining so now my URL looks like this:
/members?location=london&agefrom=18&ageto=40
I am on the right track to assume this? or should I just stick with jquery to rewrite the action parameter for an seo friendly URL?

Categories