I already use this htaccess rule already and it works
RewriteRule ^community/profile/([^/]+) community/profile/profile.php?id=$1
So, when I'm on a profile page for example website.com/community/profile/example-user, I will be able to extract the user from the id=$1 part.
I need to extend this to two folders, where example-user can have 2 sub-folders, like website.com/community/profile/example-user/folder1/ and website.com/community/profile/example-user/folder 2/
I tried the following rule:
RewriteRule ^community/profile/([^/]+)/([^/]+) community/profile/profile.php?id=$1&car=$2
Unfortunately though, this breaks the whole rule and it doesn't work. I got the idea from this post https://webmasters.stackexchange.com/questions/65785/rewrite-up-to-three-folders-into-three-query-string-parameters-to-a-php-script
Also, I tried
RewriteRule ^community/profile/../([^/]+) community/profile/profile?id=$1&car=$2
Also didn't work.
To make things clear, if one only goes to the 'first' folder, in this example it is /example-user/, the car variable should just be empty. How can I write this rule so that it will put that second 'folder' into a second variable so I can extract it later in my php file?
Make sure to use anchor $ for precise matching in regex
Include dot in your character class to avoid matching it again
Rules :
RewriteRule ^community/profile/([^/.]+)/?$ community/profile/profile.php?id=$1 [L,QSA,NC]
RewriteRule ^community/profile/([^/.]+)/([^/.]+)/?$ community/profile/profile.php?id=$1&car=$2 [L,QSA,NC]
Related
I'm trying to create some 'friendly URL' to my site. But I'm not sure what to do. Below as my site is structured:
To enter a page for example: localhost/study/dashboard/client/network.php
I wanted to write to localhost/study/network. Following the same format to some other pages. I wanted to block direct access to critical files, such as user-proc.php, api.php for example.
What I tried to do:
RewriteEngine On
RewriteRule ^study/network?$ network.php
I put a .htaccess file in each directory (administration, client), but that blocked my website for full. I could not even more access for localhost/study/administration/network.php.
To rewrite from /study/network to /study/dashboard/client/network.php, you can just put the two URLs, pattern and substitution, in a RewriteRule
RewriteRule ^study/network$ /study/dashboard/client/network.php [L]
This is for this specific case. To do this for any combination, you must capture the two parts from the request URI pattern and then use this in the substitution part
RewriteRule ^(.*?)/(.*?)$ /$1/dashboard/client/$2.php [L]
This looks for any two component part (...)/(...) and uses this - $1 and $2 - in the replacement.
To prevent access to some URI, you do almost the same, but omit the substitution part with a - (dash), and use the flag F|forbidden
RewriteRule user-proc.php - [F]
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?
I was hoping someone could help me out. I'm building a CRM application and need help modifying the .htaccess file to clean up the URLs. I've read every post regarding .htaccess and mod_rewrite and I've even tried using http://www.generateit.net/mod-rewrite/ to obtain the results with no success. Here is what I am attempting to do.
Let's call the base URL www.domain.com
We are using php with a mysql back-end and some jQuery and javascript
In that "root" folder is my .htaccess file. I'm not sure if I need a .htaccess file in each subdirectory or if one in the root is enough.
We have several actual directories of files including "crm", "sales", "finance", etc.
First off we want to strip off all the ".php" extensions which I am able to do myself thanks to these posts. However, the querying of the company and contact IDs are where I am stuck.
Right now if I load www.domain.com/crm/companies.php it displays all the companies in a list.
If I click on one of the companies it uses javascript to call a "goto_company(x)" jQuery script that writes a form and submit that form based on the ID (x) of the company. This works fine and keeps the links clean as all the end user sees is www.domain.com/crm/company.php. However you can't navigate directly to a company.
So we added a few lines in PHP to see if the POST is null and try a GET instead allowing us to do www.domain.com/crm/company.php?companyID=40 which displays company #40 out of the database.
I need to rewrite this link, and all other associated links to www.domain.com/crm/company/40
I've tried everything and nothing seems to work. Keep in mind that I need to do this for "contacts" and also on the sales portion of the app will need to do something for "deals".
To summarize here's what I am looking to do:
Change www.domain.com/crm/dash.php to www.domain.com/crm/dash
Change www.domain.com/crm/company.php?companyID=40 to www.domain.com/crm/company/40
Change www.domain.com/crm/contact.php?contactID=27 to www.domain.com/crm/contact/27
Change www.domain.com/sales/dash.php to www.domain.com/sales/dash
Change www.domain.com/sales/deal.php?dealID=6 to www.domain.com/sales/deal/6
(40, 27, and 6 are just arbitrary numbers as examples)
Just for reference, when I used the generateit.net/mod-rewrite site using www.domain.com/crm/company.php?companyID=40 as an example, here is what it told me to put in my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^crm/company/([^/]*)$ /crm/company.php?companyID=$1 [L]
Needless to say that didn't work.
OK here is an updated based on the help received from Gohn67 below
It is working with the exception of a small bug I can't seem to figure out. I have created the .htaccess file in the "crm" directory. Here is the code:
RewriteEngine On
RewriteRule ^test/([\d]+)$ /crm/company.php?companyID=$1 [L]
This rewrites www.domain.com/test/40 to www.domain.com/crm/company.php?companyID=40 so it's very close to what I need.
The bug is that I cannot replace "test" with the word "company" in my RewriteRule. I do not know why. I can put anything but the word "company" in there; even the names of other PHP files in the "crm" directory such as "contact" or "add-contact". As a further test I actually renamed company.php to test.php and changed the RewriteRule to:
RewriteRule ^company/([\d]+)$ /crm/test.php?companyID=$1 [L]
which worked.
Yeah, the generated rewrite looks kind of strange there. I'm not sure what it is trying to match here ([^/]*).
Here is an example that may work for you. I tested these on my system.
RewriteEngine On
RewriteRule ^crm/dash/?$ /crm/dash.php [L]
RewriteRule ^crm/company/([\d]+)/?$ /crm/company.php?companyID=$1 [L]
RewriteRule ^crm/contact/([\d]+)/?$ /crm/contact.php?contactID=$1 [L]
This is only a few of your routes as an example. I admit that they could be more robust though, because doing this way will lead to a lot of rewrite rules, some of which you could elminate with better regex patterns. But hopefully this gets you started.
Here are some updated rewrite rules taking into consideration a subdirectory. It also fixes a a mistake from above:
RewriteEngine On
RewriteBase /crm
RewriteRule ^dash/?$ dash.php [L]
RewriteRule ^company/([\d]+)/?$ company.php?companyID=$1 [L]
RewriteRule ^contact/([\d]+)/?$ contact.php?contactID=$1 [L]
If I have a URLhttp://www.domain.com/listing.php?company_id=1 is it possible for me to re-write that to http://www.domain.com/company-name by using that id to pull the name from the database.
Or do I have to change listing.php to make it ?company_name=company-name
If anyone can point me in the right direction that would be great.
Thanks!
A map function allows using no id in the url at all, while still using the id in the rewritten url to do the database lookup.
http://www.domain.com/another-one
maps to
http://www.domain.com/listing.php?company_id=3423
Here is how I use map text files, which I generate from a database query. I believe mod_rewrite also does mapping to a database directly, of which I'm unfamiliar (maybe someone can provide that answer). I use Helicon Tech's isapi_rewrite v3, which works like mod_rewrite, so this should work for you.
Sample map file named map_company.txt
some-company 12
another-one 3423
freds-fill-dirt-and-croissants 44
The rewrite rules:
RewriteMap map_company txt:map_company.txt [NC]
RewriteCond ${map_company:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^/(.*) /listing.php?company_id=${map_company:$1} [NC,QSA,L]
RewriteMap assigns the map_company.txt text file to a variable named map_company (named the same just to be consistent).
RewriteRule is doing the work. It captures everything after the slash into $1, then rewrites it to your listing.php url. The ${map_company:$1} is looking up the url in the map file, and returning the id.
RewriteCond is just doing a double-check to see if the listing is there. The $1 is coming from the RewriteRule url, the NOT_FOUND is a default value if not found, and the condition is if it's not-NOT_FOUND (a little tangled - but it's just checking if it's in the file). If it's in the file, the RewriteRule will be run. If it's not in the file, it skips the RewriteRule, and falls through to more rules (perhaps to a page-not-found or some other default).
You'll need the id in both the urls, but only use the full name in the pretty link for the user.
The following would rewrite http://www.domain.com/42/company_name to http://www.domain.com/listing.php?company_id=42 and just discard the company name.
RewriteEngine on
RewriteRule ^([0-9]*)/([A-Za-z0-9_]*)/$ /listing.php?company_id=$1 [L]
Note that a user could also visit http://www.domain.com/42/wrong_name and still land on the right page with the right company name. If this isn't desired you could change the rule to /listing.php?company_id=$1&company_name=$2 and check for equality in listings.php
I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
I have been referring to this link and I have found related threads:
Mod_rewrite flat links
Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
Thanks for your help.
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.