I have my site as localhost/tutorials/rewrite/news.php on this page are titles from news articles that are in my database. Each title is a link to read the article. The link is
'' . $title1. ''
The read-news.php page gets the url and uses it in an sql statement to get the article from the database
$url = $_GET['url'];
$sql = "SELECT * FROM news WHERE url = '$url'";
My link on the news.php page and the url on the read-news.php looks like this
localhost/tutorials/rewrite/read-news.php?url=the-news-today
How can i get it to look like
localhost/tutorials/rewrite/read-news/the-news-today.php
I have used the following htaccess code which by looking at other examples i thought should be enough to fix it
RewriteRule ^read-news/(.*).php /read-news.php?url=$1 [PT]
Any help please
edit: your RewriteRule needs some work too. try this:
RewriteRule ^read-news/(.*)\.php$ /read-news.php?url=$1 [PT]
(you need to use a "$" to signal the end of your rewrite and escape the . before "php" with a backslash)
Also, you should link to your pages how you want them displayed:
href="localhost/tutorials/rewrite/read-news/the-news-today.php"
The .htaccess won't magically change the urls for you.
Also if you want to be able to access your query string variables, i would add QSA to your flags: [PT,QSA]
that way you can still access your url variable with $_GET['url'].
Do it how Wordpress does it. Rewrite everything into index.php and parse the url from script.
Related
I have created the website like WOWcrush http://phone77.ml I want the url be like phone77.ml/Naveen in title it should have my name but there come ? at the last of the URl like http://phone77.ml/?Naveen only if use ? it works fine or it shows 404 error please help
You will need to use the .htaccess file in order to point /Naveen to the coresponding get var.
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([^/.]*)/?$ index.php?section=$1 [L]
or you can be more specific like this
RewriteRule ^/Naveen$ index.php?section=Naveen [L]
$_GET['section'] will contain Naveen and you can use that in your code to show the correct view
By default everything what goes after the '?' symbol is parameters in url. It can be changed, but for that is necessary to properly configure a web server (e.g. Apache or Nginx).
Also can be necessare to made some changes in code or/and configuration of your application.
You can see the question mark as a 'where'. So what you are saying is;
http://phone77.ml/ 'WHERE' Naveen. It all depends on how you build your site, but apparently you have a clause looking for Naveen in the URL. Maybe something like; if(isset($_GET['Naveen'])){...}.
You could also have separate documents for each you pages, then you would link to to that document instead and the URL would look like http://phone77.ml/Naveen.php f.ex.
You might also be interested in looking up the .htaccess document https://httpd.apache.org/docs/current/howto/htaccess.html where you can manipulate the URL from.
Clean url in php using htaccess
Here is an example
http://www.example.com/Mobiles/index.php?idd=4
and i want result like this
http://www.example.com/Mobiles
Please help
This is called URL Rewrite. If your page links are dynamic, like extracting data from database which is mostly the case in e-commerce sites, then the best approach is to append the id at the end of URL. This way you can fetch the data from database. Like in your case, your new URL might look like:
http://www.example.com/Mobiles/4
When user will visit this link .htaccess file will internally rewrite this URL to:
http://www.example.com/Mobiles/index.php?id=4
In this way you can then retrieve id from your PHP like this:
$id = $_GET['id'];
or:
extract($_GET);
This extract function will create the variables automatically from the parameters name and you can access it directly with $id variable.
Here is the .htaccess code:
RewriteEngine On
RewriteRule ^Mobiles/(\d+)$ http://www.example.com/Mobiles/index.php?id=$1
In case if you don't need URL like http://www.example.com/Mobiles/4, then use this:
RewriteEngine On
RewriteRule ^Mobiles$ http://www.example.com/Mobiles/index.php?id=4
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.
I am writing the following rules in htaccess file to change the query string as directory structure
RewriteRule ^dashboard/([0-9]*)?$ dashboard.php?user_id=$1
is used to rewrite the url. It is working fine on
localhost/project/dashboard // (dashboard.php)
and all links are as
localhost/project/css/style.css
localhost/project/js/script.js
But When I append an id
localhost/project/dashboard/1 // (dashboard.php?user_id=1)
It changes all the links as
localhost/project/dashboard/css/style.css
localhost/project/dashboard/js/script.js
Why it is appending the dashboard to all links
How is the style.css referenced in your html file?
If you have it like this href="css/style.css", the HTML doesn't know you're rewriting, thinks /1 is a folder and will look in dashboard/css/style.css
Any system that uses url rewrite usually has to write all the path to the styles and scripts to avoid this. so you will have to reference your style like this
href="http://localhost/project/css/style.css"
if you have a production and development environment it will help you to have a variable like
if($SERVER['SERVER_NAME']=='localhost'){
$BASE_URL = "http://localhost/project/"
}else{
$BASE_URL = "http://mydomain.com/"
}
and put that before any call to css, scripts or images
;)
It's because you "tell him" to do that.
RewriteRule ^dashboard/([0-9]*)?$ dashboard.php?user_id=$1
// ^here you tell him to print that "dashboard"
Of course other links works - you don't even match them with that rule.
I found you something here, scroll down to the title "Strip Query Strings". There, it say you to do this:
RewriteCond %{QUERY_STRING} example=
RewriteRule (.*) http://www.domain.com/$1? [R=301]
Just, of course, change that url to your own.
I'm trying to write an .htaccess file that will make my URLs more attractive to search engines. I know basically how to do this, but I'm wondering how I could do this dynamically.
My URL generally looks like:
view.php?mode=prod&id=1234
What I'd like to do is take the id from the url, do a database query, then put the title returned from the DB into the url. something like:
/products/This-is-the-product-title
I know that some people have accomplished this with phpbb forum URLs and topics, and i've tried to track the code down to where it replaces the actual URL with the new title string URL, but no luck.
I know I can rewrite the URL with just the id like:
RewriteRule ^view\.php?mode=prod&id=([0-9]+) /products/$1/
Is there a way in PHP to overwrite the URL displayed?
At the moment you're wondering how to convert your ugly URL (e.g. /view.php?mode=prod&id=1234) into a pretty URL (e.g. /products/product-title). Start looking at this the other way around.
What you want is someone typing /products/product-title to actually take them to the page that can be accessed by /view.php?mode=prod&id=1234.
i.e. your rule could be as follows:
RewriteRule ^products/([A-Za-z0-9-])/?$ /view.php?mode=prod&title=$1
Then in view.php do a lookup based on the title to find the id. Then carry on as normal.
One way to do it, would be just like most mvc frameworks. You can redirect all your pages to the same index.php file, and you use your script to determine which page to load.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
and your php file will have a script like this one:
// get the url
$uri = (isset($_SERVER['REQUEST_URI']))?$_SERVER['REQUEST_URI']: false;
$query = (isset($_SERVER['QUERY_STRING']))?$_SERVER['QUERY_STRING']: '';
$url = str_replace($query,'',$uri); // you can edit this part to do something with the query
$arr = explode('/',$url);
array_shift($arr);
// get the correct page to display
$controller =!empty($arr[0])?$arr[0]:'home'; // $arr[0] could be product/
$action = isset($arr[1]) && !empty($arr[1])?$arr[1]:'index'; // $arr[1] can be product-title
}
of course you will have to work this code to fashion your application
I hope this helps
One way would be to output a Location: header to force a redirect to the chosen URL.