I'm putting a search engine on my site, and the search box appears on several different pages.
The output looks like this: http://mysite.com/mypage.php?bluepart=search&keywords=dogs&go=Go
I'm trying to do an .htaccess mod rewrite to where any page that passes these variables will get redirected to search_results.php. The bluepart=search and go=Go will always be the same, but keywords can be any number or words. Also, some of my pages are .html and some are .php, when I refer to any page that passes the variables.
Take a look at the RewriteCond option. It allows you to specify a rewrite if an regular expression matches the Query string (QUERY_STRING).
Related
I'm creating a simple, wordpresslike cms and keep all parts of a page in a table and load them using a template page named view.php following by a $_GET variable, for example:
sky.com/view.php?article=how-to-do-something
But as I can see on wordpress sites there is no 'view.php' template file and $_GET variables inside url. There is pure domain name and title of an article.
I suppose this is a better approach for SEO engines.
What is the general way to do this and how can I use the same. Is there a function to create a file on fly, or maybe a hidden file system...
The same is with stackoverflow.com. There is no view template inside url, but I'm rather sure it uses database table for storing parts of a page.
I tried with .htaccess file and this accepted solution but got the error 500 - internal server error.
By way of explanation; what stackoverflow.com probably does is something like this:
URL:
https://stackoverflow.com/questions/44654672/cms-without-view-template-inside-url
Actual URL is first rewritten using mod_rewrite so that 1) the missing .php is added, and 2) the GET parameters are set:
RewriteEngine on
RewriteBase /
RewriteRule ^questions/([0-9]+) questions.php?qid=$1 [NC,L]
And so the questions.php page loads the question with id 44654672.
The above mod_rewrite says: take the URL, starting with questions/ and send that URL to questions.php, as well as saying: After questions/ take the number value and use that as the question id GET variable so that the PHP page loads the correct question from the database.
The wording in the URL is purely for SEO purposes.
You will notice that https://stackoverflow.com/questions/44654672 will also load your question correctly (*), but if you change the number value (even while keeping the words the same, ( such as https://stackoverflow.com/questions/44351172/cms-without-view-template-inside-url ) another question will be loaded from the database.
Putting the exact URL link into my answer makes it display the question title rather than URL
Think of it as the mod_rewrite is doing a find and replace search on the URL string. That's all.
So, I ran into an issue with query strings and rewriting them to seem "prettier"
My website is at: localhost/admin/ so when uploaded to my server it is also under the /admin directory, and the .htaccess file looks like this:
RewriteEngine On
# redirect URL with empty query string to index.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?(.*)$ /admin/index.php?$1 [NC,L]
Unfortunately, my design for the admin panel all of the pages are linked to the index.php file. So when I originally used to request http://localhost/admin/?users it would show a list of all of the users. I wanted to make this prettier so I used the above htaccess rules and came up with http://localhost/admin/users. Two issues arose with this:
The css files within the admin folder under css/ could not be accessed so none of the styles were being applied.
This ?users page contains pagination using the GET parameter in php as well, so it looks kind of like http://localhost/admin/users&page=1 and used to look like http://localhost/admin/?users&page=2.
So basically what I wanted to do was make the code look more like this: http://localhost/admin/users/page/1. Unfortunately I cannot hard code the users parameter in because I have other sections like "websites" and I will also have a edit users page with id's etc. So I was wondering if someone could help me with the best way to approach this problem or how I can fix the above htaccess rules? The only thing that kind of helped was: .htaccess mod_rewrite Unknown number of Variables of a GET form but I am still lost on how to implement it for multiple empty GET variables at the front of the url.
Thanks!
Here's what I do,
First for css part use <base href="domain.com/"> in head section of your pages.
You are redirecting full url to query string of index.php and I am assuming you are retrieving is as $_SERVER['QUERY_STRING'];.
So I suggest you that you change your pagination url to http://localhost/admin/users/page/1.
And after that when you retrieve them in index.php explode the string using explode function,
$arr = explode('/',$_SERVER['QUERY_STRING']);
Inside your $arr you will have all your values which you got them from query string.
I have urls that look like http://WEBSITE.com/?page_id=53?&nid=3104&lid=3100
As you can guess it is an ugly wordpress site, search function pulling up a result with id nid=3104 and lid=3100 . Both numbers are necessary.
I would rather URLs look like http://WEBSITE.com/Chicago-Shelter-name
or http://WEBSITE.com/shelter/Chicago-Shelter-name
How can I get an .htaccess file to read the search result from a PHP script (that will pull if from SQL using the NID and LID numbers) and when the name comes back as "Chicago Shelter name" rewrite the url http://WEBSITE.com/Chicago-Shelter-name
SOLVED
Not easy.
.htaccess needs:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^custom/name/?$ newfile.php?nid=3104&lid=3100 [NC,L]
</IfModule>
This being wordpress required a replica page of the entire search result. The menu and sidebar will not reflect any updates, but I had to break out of wordpress.
Copied the source of the search result, removed the result, added the PHP that displays the result.
All inside of "newfile.php"
The above .htaccess file works.
If http://WEBSITE.com/custom/name" is entered, it will display that entered URL
but in reality secretly display the result of "http://WEBSITE.com/newfile.php?nid=3104&lid=3100"
This is what I wanted. Not the best, and now I must create lines of code for every single possible item in search. But it works.
I have an index page that can be accessed from the URL http://mysite.com. I can input query strings ?type=small, ?type=medium or ?type=large so that different results will be displayed. However, I want users to be able to access the site with the URLs http://mysite.com/small, http://mysite.com/medium and http://mysite.com/large respectively instead without creating separate pages.
Can this be done and how?
RewriteEngine On
RewriteRule ^(small|medium|large) /?type=$1
Lest say i have a link and when some one click on that link it redirect the person to the following page
http://localhost/install.php?act=go
now i just want to remove everything after ? so that person doesn't see the actual query string behind it or is there any way to encode the characters and make it look different in browser
You can add a mod_rewrite rule to your htaccess something like
RewriteRule ^install/([A-Za-z0-9-_]+)$ /install.php?act=$1
that way /install/go takes you to the destinantion