Can I rewrite a url by using the value 6 from ?cat for example, and checking my MySQL database for the value 6 category name html using PHP & MySQL if so how?
Current url.
http://localhost/index.php?cat=6&sub1=8&sub2=24&sub3=81
New search friendly URL displayed in browser.
http://localhost/html/basics/forms/select-tag/
Check out mod_rewrite.
You need to open up the .htaccess file(if it does not exist, create it) and include the following lines. Note that RewriteEngine On should be only written once.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^html/basics/forms/select-tag/([0-9]+)/?$ index.php?cat=$1 [NC,L]
Yes. Basically you will want to pass the entire url through a router script using mod_rewrite. The .htaccess file should have something like this:
RewriteEngine On
RewriteBase /
#if it's an existing directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
#...or an existing file
RewriteCond %{REQUEST_FILENAME} -f
#serve it up
RewriteRule ^(.+) - [PT,L]
#else, direct to index.php
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Then, in index.php you can have something like this:
$request = explode('/', $_GET['url']);
Which would have all your clean url segments in the $request array. Once you have those values, you can connect to the database, find what the url represents, and output the page. If it's not found, you can send a 404 header with a "Page not found" message.
header('HTTP/1.0 404 Not Found');
echo '<p>Page not found</p>';
So that's the basic clean url technique. If the url is the earlier, messy one, you would just add some logic to check for that, and redirect to the correct clean url:
if(isset($_GET['cat'])){
//put some logic here
header("Location: http://localhost/".$the_clean_url);
}
This basic technique should solve your problem with some tinkering.
Related
I've been trying the past 2-3 hours to get this working but I simply don't have enough knowledge of regex.
I'm trying to make an url shortener, so I want to point a url like domain.com/d5Ds93X to something like domain.com/view.php?s=d5Ds93X
I've been messing around with my .htaccess file and have this now:
RewriteEngine on
RewriteBase /
RewriteRule /(.*?)$ /view.php?s=$1 [L]
And that sort of works, it points to the right file but the string (d5Ds93X) isn't passed on. My view.php file looks like this (for testing purposes):
<?php
if (isset($_GET['s']) && ctype_alnum($_GET['s'])) {
echo $_GET['s'];
}
?>
When visiting the url I just get a blank page. But if I echo anything before the if rule it does get displayed so the page works just fine. Does the data of the GET even get passed on? Or should I add code that detects the data in the url?
Use it like this:
RewriteEngine on
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ view.php?s=$1 [L,QSA]
Make leading slash optional so that rule works from .htaccess as well as from Apache config.
Also restrict rule to skip files and directories.
I'm sorry about the title but I can't find a good one to explain what I try to do.
I have my website : http://domaine.com .
The index.php file in it redirect to http://domaine.com/views/index.php .
Now I want to Rewrite Rule so that any url like :
http://domaine.com/something display the site http://domaine.com so that :
http://domaine.com/php redirect http://domaine.com/php/views/index.php
http://domaine.com/java redirect http://domaine.com/java/views/index.php
http://domaine.com/ruby redirect http://domaine.com/ruby/views/index.php
etc.
EDIT : I'm working on localhost so it's supposed to:
localhost/formation/php => localhost/formation/php/views/index.php
localhost/formation/java => localhost/formation/java/views/index.php
localhost/formation/ruby => localhost/formation/ruby/views/index.php
EDIT : The content of my htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ views/index.php [L,QSA]
When I access to localhost/formation/ruby/ for exemple it's working but after the loggin it look like it's always redirecting from the home page to login page to home page etc.
EDIT : I create a repertory called "ruby" and create in it a htaccess file with that content.
RewriteEngine On
RewriteRule ^(.*)$ ../$1 [L]
And it does exactly what I want. So I want to find a way to do that without having to create the repertory "ruby".
http://domaine.com/php redirect http://domaine.com/php/views/index.php
http://domaine.com/java redirect http://domaine.com/java/views/index.php
http://domaine.com/ruby redirect http://domaine.com/ruby/views/index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(php|java|ruby)/$
RewriteRule ^.*$ /%1/views/index.php
I suppose by "redirect" you mean "serve as". In case if you want a 301 redirect, use the appropriate flags for RewriteRule, e.g. [R=301,L].
Wrapper
In case if you are trying to implement a wrapper script which is supposed to handle different request URIs, you should process the requests with a single script and pass the URI path parts as query string parameters. For instance:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(formation|another_type)/(php|java|ruby)/$
RewriteRule ^.*$ /wrapper.php?type=%1&lang=%2
With this configuration, you can handle requests like http://your-site.com/formation/ruby/ with a single script. You can access the query string parameters using the $_GET superglobal variable, e.g. echo $_GET['lang'];.
Ultimately, you can process all requests with a single wrapper:
RewriteRule ^.*$ %{DOCUMENT_ROOT}/wrapper.php
Within the wrapper you will have to examine the contents of the $_SERVER superglobal. The REQUEST_URI and REQUEST_URI values are particularly useful.
I am creating SEO friendly URLs for my website. I have a file notfound.php to which I am redirecting all the URLs. Now, I want notfound.php to search for the URL in database and generate the corresponding content.
But the problem is that in the URL it shows is http://some_url/notfound.php
I don't want this URL to be displayed in the address bar, instead I want the original one.
For example,
http://some_url/hello/world is redirecting to http://some_url/notfound.php using ErrorDocument 404
and notfound.php is serving the content
But how to show http://some_url/hello/world in the URL instead of notfound.php? And, is this the right way to redirect and rewrite while matching the URLs from a database?
you must have mod_rewrite enabled, then you can try:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ notfound.php [L]
the first two lines ensure that you can still load things that exist (like js and css files that you may need)
ErrorDocument does an external redirect when you give it a complete URL. The doc tells you how to do an internal redirect.
http://httpd.apache.org/docs/current/custom-error.html#configuration
I am using a simple htaccess file to rewrite urls of my project. I rewrite the whole url in a get variable, then use php to parse it.
My problem is when I try to send redirect headers, I can't seem to get it right.
For example, say I am viewing the page
localhost/myProject/home
With the header
header('Location: login');
it redirects to
localhost/myProject/home/login
When I try
header('Location: /login');
it takes me to
localhost/login
which does not exist and I get a 404 error.
So far the only way I have found to work is when I do
header('Location: http://localhost/myProject/login');
but I don't think that is a good option because when my site goes live I will have to change every single header.
Is there a better way to do it?
My .htacces file is:
RewriteEngine On
RewriteBase /myProject/
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php?get=$1 [L]
you can do it like this : Use base url variable for that.Define that variable global or in config file.You have to only change that variable one time:
$base_url = "http://google.com"; or define('base_url', 'http://google.com');
I've been trying to rewrite my URL's with a htaccess file.
My project is using my index.php as it's basic controller.
I fetch the pages from the database with the get-value "naam".
Example:
localhost/YourDesigns/YDCMS/index.php?naam=home (this shows the homepage)
localhost/YourDesigns/YDCMS/index.php?naam=about (this shows the about page)
Now i want to rewrite the URLS to be shown like this:
localhost/YourDesigns/YDCMS/home (this shows the homepage)
localhost/YourDesigns/YDCMS/about (this shows the about page)
I have done some htaccess stuff myself and i've successfully removed the "index.php" from the url, but the "naam" still remains.
How do i remove this?
This is my current htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=/$1 [L]
Thanks in advance :)
I think your way to see the process it's not totally right, if you want to show the url like you say localhost/YourDesigns/YDCMS/home you have to first replace the url inside your html content to that format, then by using a RewriteRule you call the correct path internally:
RewriteRule ^desired_url_path/([a-z0-9\-]+)$ /base_url_path/index.php?naam=$1 [L]
this way when an user click on a link the Apache server will use the above regex to convert the url by a rule that basically says : everything after the base url is aparameter value to be passed on the index.php.
Naturally the regex can be modified to suit your needs, the one i've written above it's a basic string pattern.