I am creating my menu dynamically from a database. I started by appending an id to the urls which I used to identify the page in the database and then get all urls from the subpages and so on..
so I had a lot of ids like this www.page.com/somefolder/pagename.php?id=10, where 10 is the id of the page and is the parent_id for a number of other pages.
Now I use mod rewrite to get nicer urls. So the above url is now www.page.com/pagename.php , which is mapped to the original url.
My pagenames are all gonna be unique.
The question: Do I have to do this for each and every url, since each url needs an id as query string or is there a better way to combine mod rewrite with dynamically generating a menu?
No, not for every URL, but you will need to do this the other way around.
Turn the rewrite engine on
RewriteEngine on
Build a path to match against using a dynamic 'case' in brackets..
RewriteRule ^/?somefolder/page/([0-9]+)/?$
.. complete the rule with the rewritten URL, using $1 for the 'case'
/somefolder/pagename.php?id=$1 [L]
Put it all together using [L] to signify the 'last' rule to execute.
RewriteRule ^/?somefolder/page/([0-9]+)/?$ /somefolder/pagename.php?id=$1 [NC,L]
This will rewrite /somefolder/page/12345/ to /somefolder/pagename.php?id=12345
Related
I am have a clear URL based system so the categories will be shown like this
http://www.mysite.com/category/23/cat-name
Inside the categories page I have some sorting and pages options such as sorting by latests and lower price. Also, I have a pages navigation system
The problem is when the request is happening inside the page the $_GET[] doesn't show the variables I need. However it shows in the URL as
http://www.mysite.com/category/23/cat-name?page=3
The $_GET variable only shows the id of the category which is in our case now = 23 and ignored the page number which is in the url.
.htaccess content
RewriteEngine On
RewriteRule ^category/([0-9]+)/[a-z][-a-z0-9]*$ cat.php?id=$1
You need to include the QSA flag at the end of your rule. This appends any existing query string to the one that you're constructing in the rule itself:
RewriteEngine On
RewriteRule ^category/([0-9]+)/[a-z][-a-z0-9]*$ cat.php?id=$1 [L,QSA]
So the rewritten URI should end up being: /cat.php?id=23&page=3
Add QSA(query string append) at the end of your rewrite rule.
RewriteEngine On
RewriteRule ^category/([0-9]+)/[a-z][-a-z0-9]*$ cat.php?id=$1 [L,QSA]
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.
I'm using a custom built CMS and I need to rewrite the urls because currently they take the page ID and name of the page into the url.
/pages/1411760825/Facilities is an example of the current url
/Facilities is what I want it to show up as in the URL bar
The pages are linking to each other based on their ids and titles and I want it to not do that anymore.
This is the current .htaccess rule
RewriteEngine on
RewriteRule (.*)\.xml(.*) $1.php$2 [nocase]
I know I need to add a line below to rewrite what I want it to be, but I'm unsure how to achieve this.
We had a major site rework and now our new site is PHP based. We have some sites which have our old pages linked to them which were asp pages in the past. I want to create a htaccess rule but I have failed to get the desired result. If anyone can help it would be great.
The incoming pages will be in the format
http://www.mysite.com/tour.asp?id=1234
where 1234 is dynamic and can vary. I want to redirect this to the page viewer.php and change the query string variable id to tourid so the page requested should be
http://www.mysite.com/viewer/1234
I have another rule in htaccess which routs the above url to
http://www.mysite.com/viewer.php?tourid=1234 (this is working)
I need a rule by which the 1st url is routed to the 2nd one hoping it will open the 3rd url. How can i achieve this?
Since I am new to htaccess, can someone help me around how to achieve this routing? I want to clear here that the server now is php and I have to accomplish this using htaccess only. I followed other similar questions but found none that answers my query.
UPADTE - I got it wrong first time, here's the corrected version.
# Match and capture the id value from the URL
RewriteCond %{QUERY_STRING} id=([0-9]+)
# Redirect by passing the ID to the new site - the ? at the end removes existing query string
RewriteRule tour.asp http://www.mysite.com/viewer/%1? [R=301,L]
I've assumed the IDs are numeric only.
The source for this answer is at https://httpd.apache.org/docs/2.2/mod/mod_rewrite.html - search for "Modifying the Query String" to find the info about removing the query string.
My website functions a little like 9gag or damnlol, etc.
when the user visits an item page, for example: site.com/104-funny-thing, the rewrite rule redirects to view.php?id=104-funny-thing. Here the URL is stripped away of everything after the first '-'. So we are left with the id only, in this case: 104, and the query gets the item.
Here is the Rule:
RewriteRule ^([A-Za-z0-9-]+)$ view.php?id=$1
What I want is that the same URL format could be used to open up user profiles as well. For example: site.com/salman, this URL should open up profile.php?username=salman.
I thought of doing this via PHP. Where I can use the same file, view.php, for both purposes. Once the parsing of the URL is done, if I get a numeric value, I would include the item.php page and query and show data accordingly. If it's not numerical, then I ca include the profile.php page and show the profile instead. This should theoretically work flawlessly, but what I wanted to try was that if there was a way to redirect using the .htaccess directly, I would prefer to use that option, as the previous option requires extra work to sort out some issues that the solution causes.
Someone good at htaccess (regex code?) might be able to offer a solution. Here is some further explanation:
Usernames can contain numbers, but not only numbers
Username URLs cannot contain '-'
Item IDs cannot contain alphabets
URLs 104-funny, 104- and 104, all work for view.php
Thank you for your help in advanced!
#article urls
RewriteRule ^([0-9]+)(-.*)?$ view.php?id=$1
#profile urls.
RewriteRule ^([A-Za-z0-9]+)$ profile.php?username=$1
I have a MySQL database with 5000 items. The table is structured as
id
product-name
content
product-url
You can access a specific product page by going to www.site.com/products/index.php?id=123. The index.php file uses PHP to GET the ID to search the MySQL database and return the appropriate product name and content.
How can I make it so instead of www.site.com/products/index.php?id=123, the URL is rewritten or redirected to www.site.com/products/product-url? If I do it with .htaccess, will the index.php file still be able to know what ID to use to look up the product-url if the ID is not in the URL? Or is there some way to map URLs with PHP and MySQL?
(I'm a noob when it comes to this stuff, so any help will be much appreciated. Thanks.)
You can use .htaccess and a RewriteRule. The following will redirect an SEO URL to index.php with $_GET['product-url']. You will need to modify your code to either lookup the product id or use the product-url directly. Both should be unique in your products table.
RewriteEngine On
RewriteBase /
# redirect product page from SEO URL
RewriteRule ^products/([\w\d-_]+)/?$ products/index.php?product-url=$1 [L]
Note: I've made the product_url to only allow letters, numbers, dashes and underscores. I suggest such a restriction. In addition, the trailing slash is optional. Futhermore, this will not reappend a query string. If you need that you can add the QSA flag to the RewriteRule.