htaccess tagging system custom htaccess - php

I'm having trouble understanding the .htaccess file and the formatting; basically my question is how would I accomplish a link like this; /tag/hashtag/category
so I would have one index file in the tag directory which would grab the hashtag and category? and regardless of the input of the hash-tag and the category it would still take me to the index file in the tag directory.
Sorry for the real basic question/request.

Here is the content of your .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^tag/([^/]+)/([^/]+)$ /tag/index.php?hash=$1&cat=$2
([^/]+) means that we want to capture a string witch contains characters that are not /, so we get all characters between two slashes.

Related

url search for file in subdirectory instead from public_html using htaccess

I am working on a website in which I write code for htaccess but the thing which I wanted to do is not happening. I have url which is:
http://www.example.com/demo.php?id=234&title=ask%20me%20a%20question
I converted to below url using htaccess:
http://www.example.com/234/ask%20me%a%question
htaccess code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([0-9]{4})/([a-z]+)/$ demo.php?url=$1&url2=$2
So. the problem is converted url is search for related file in subdirectory instead of server root i.e; public_html. I want to know how could this problem will solve.
Plz help me. Thanks.
The second parameter in your request requires that characters other than a-z be included, but you are limiting it to a-z.
In addition, you are requesting 234 in the URI, but checking for 4 numbers in the first parameter.
As such, change your rule to the following:
RewriteRule ^([0-9]{3,4})/([^/]+)/?$ demo.php?url=$1&url2=$2 [L]
Changes
Allow 3 or 4 numbers in the first parameter. If you want to be more flexible, you can change it to ([0-9]+).
Check for all characters other than / in the second parameter.
Make the trailing slash optional using /?.
Add the L flag to stop rewriting if the rule is matched (always good to have for when you add other rules).

Friendly URLs and htaccess levels

I have this .htaccess
RewriteEngine On
RewriteRule ^custom index.php?page=value [NC,L]
It works if index.php and .htaccess are located in the same folder.
I'd like to place the .htaccess file in the root and make the content placed in a newFolder react to the htaccess rules. Say:
.htaccess -> root level
index.php -> root/newFolder level
I did try:
RewriteEngine On
RewriteRule ^custom newFolder/index.php?page=value [NC,L]
No reaction at all (404) :(
I guess the solution must by simple, but...
Any help will be appreciated.
Let's make sure we're on the same page. ;) In my server root directory I have a .htaccess file and in that file I have:
RewriteEngine On
RewriteRule ^(.+/)?custom $1index.php?page=value [NC,L]
The RewriteRule has three components: a match pattern, a target, and a list of flags. The match pattern can contain plain text or a regular expression. The target can contain plain text or text with intermingled variables captured in the regular expression match pattern. Both match pattern and target are relative to the directory the .htaccess file is located in.
The match pattern is compared to the text in the URL that is after the current directory. If there's a successful match then the target is called from the directory the .htaccess file is in. If information is captured in the match pattern then the variables in the target are first interpolated in the target.
In this example the .htaccess file is in the server root therefore the match pattern is compared to the URL text after: http://localhost/
To test it out, here's the PHP code I put in an index.php file:
<?php
echo 'page value: ' .$_GET['page'] . '<br>';
echo 'script loaded: ' . $_SERVER['PHP_SELF'];
?>
When I navigate to http://localhost/custom in my browser: the regular expression match pattern is compared to the string "custom", there's a successful match so the target index.php?page=value is executed, and I get the index.php page in the root directory loaded. The displayed page looks like this:
page value: value
script loaded: /index.php
Next I create a new directory called "newFolder" in the server root and copy the index.php file to the newly created directory.
When I navigate to http://localhost/newFolder/custom in my browser: the regular expression match pattern is compared to the string "newFolder/custom", again there's a successful match, this time the target newFolder/index.php?page=value is executed, and I get the index.php page in the newFolder directory loaded. The displayed page now looks like this:
page value: value
script loaded: /newFolder/index.php
So, likewise, I can create any number of directories in the root directory and new index.php files in those directories, and when navigating to "custom" in those directories, this .htaccess file ought to load the corresponding index.php file in the current subdirectory.
~~~
I'm going to go out on a limb here and assume you want to load pages dynamically, therefore capture "custom" as a dynamic page name and dump it in the page field (replace "value") and send that to the index.php file in the current subdirectory.
RewriteEngine On
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.+/)?(.+)$ $1index.php?page=$2 [NC,L]
As mentioned, the parentheses capture dynamic text and dump it in the target string where the dollar sign numbers are. So the text captured in the first set of parentheses replaces the "$1" and the text captured in the second set replaces "$2". I also added the RewriteCond statement to prevent this rule from double-dipping. :) If I don't this rule gets executed for both the "custom" call AND the redirection to index.php.
So now when I navigate to http://localhost/newFolder/whoopdeedoo, the regex match pattern is compared to "newFolder/whoopdeedoo", there's a successful match, the target newFolder/index.php?page=whoopdeedoo is executed, and I get:
page value: whoopdeedoo
script loaded: /newFolder/index.php
~~~
At the risk of droning on, here's some background info. You mentioned this works in the root directory:
RewriteEngine On
RewriteRule ^custom index.php?page=value [NC,L]
But it does not work when navigating your browser to subdirectories. After reading the comments, it appears you discovered this next bit works for subdirectories, specifically "newFolder":
RewriteEngine On
RewriteRule ^newFolder/custom newFolder/index.php?page=value [NC,L]
But now it no longer works for the root directory. So you could simply include both:
RewriteEngine On
RewriteRule ^custom index.php?page=value [NC,L]
RewriteRule ^newFolder/custom newFolder/index.php?page=value [NC,L]
However there's a couple things here that jump out at me. One is that rather than hardcoding directory names in my root .htaccess I'd rather put .htaccess files in each subdirectory and take over the URL processing there.
But if for whatever reason you prefer to maintain one .htaccess file in the root directory, then, what jumps out at me is the duplication in the rules. So, alternatively, you can use regular expressions to make the subdirectory name a captured variable, like shown at the top of this answer:
RewriteEngine On
RewriteRule ^(.+/)?custom $1index.php?page=value [NC,L]
The regex capturing parentheses replace the text "newFolder/", and inside the parentheses we're saying, "capture one or more characters followed by a forward slash". Then, if there's a successful match, the value captured replaces the "$1" in the target. The reason this also works in the root is because the question mark following the parentheses makes the regex inside the parens optional. So if there's nothing before "custom" we're still good for a match. In that case the "$1" gets replaced with an empty string.
Try with this rewriterule:
# Activate RewriteEngine
RewriteEngine on
# Rewrite the URL requested by the user
# Entry: folder/clients/name/
# Output: folder/clients.php?id=name
RewriteRule ^folder/clients/(\w+)/?$ /folder/clients.php?id=$1
Explanation of this rewriterule:
First part of the rewriterule:
^ Top expression
folder/clients/ The requested URL string begins with folder/clients/
(\w+) Capture any letters that follow and stores it in $1
/? Optional backslash at the end of the URL
$ End of the expression
Second part of the rewriterule:
clients.php?id= Text string
$1 The first capture we saw in the first part

Grabbing a domain name from URL as a variable by htaccess

Imagine in my website I want to show some analytic about domains, working URL example of what I need:
http://whois.domaintools.com/google.com
As you see in the above URL, it's handling google.com as a variable and pass it to another page to process the given variable, that's exactly what I want.
So for detecting that kind of variable, here is my regex:
/^[a-zA-Z\d]+(?:-?[a-zA-Z\d])+\.[a-zA-Z]+$/
The above RegEx is simple and accepts everything like: google.com, so in my .htaccess file I have:
RewriteRule (^[a-zA-Z\d]+(?:-?[a-zA-Z\d])+\.[a-zA-Z]+$) modules/pages/page.php?domain=$1
The above rule do what I want, but it also redirects my homepage to page.php while there is nothing in the URL, forexample: http://mysitename.com is now being forwarded to page.php
How can I fix this?
Thanks in advance
It redirects also the base domain to page.php because of the regex. You are using the + on all places, the meaning of the plus is "Matches the preceding pattern element one or more times.". (http://en.wikipedia.org/wiki/Regular_expression) If you request the homepage, it redirects because all the elements are appearing zero times, like you defined in the regex.
Instead of the + you should define a minimum and a maximum amount of characters (so the zero occurrences are not evaluated). BTW, a quick search in google for "regex domain" will output a lot of results, which are tested. Use the following for example:
RewriteEngine on
RewriteRule (^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z]{2,3})$) modules/pages/page.php?domain=$1
Reference:
Domain name validation with RegEx
Update 1:
If you want to use your own regex, exchange the last "+" with {2,}. The top-level domains have usually at least 2 characters.
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.html|\.php|\.pdf|\.gif|\.png|\.jpg|\|\.jpeg)$
RewriteRule (^[a-zA-Z\d]+(?:-?[a-zA-Z\d])+\.[a-zA-Z]{2,}$) modules/pages/page.php?domain=$1

Special Characters break Generated URLs

I have an automated process which generates urls from the title of venue.
I then use the following line to within my .htaccess to get the url to redirect to the correct path
RewriteRule ^recipes/([\w-]+)/(\d+)$ ./recipes_news.php?i=$2 [L,QSA]
a typical URL looks like the link below
www.site.com/recipes/red-curry-chicken/123
Where the last part of the url is the id used to find the actual recipe information.
For some reason unknown to me, anytime a special character such as "ā" occurs, it breaks the url.
Is there something I am missing in the .htaccess code to capture special chacters?
Thanks
Try changing your regex pattern to:
RewriteRule ^recipes/([^/]+)/(\d+)$ ./recipes_news.php?i=$2 [L,QSA]

PHP - How to add a pages title to the URL? And how to create a clean url using PHP

I was wondering how can I create clean urls using PHP. Do I do this all in PHP or do I need to use mod_rewrite in some way? Can someone explain this to me in laymans terms?
Here is my current url a element link and how it looks in the browser
http://www.example.com/members/1/posts/page.php?aid=123
But I want it to read the pages title.
http://www.example.com/members/1/posts/title-of-current-page/
First you need to generate "title-of-current-page" from PHP, using this function eg:
function google($string){
$string = strtolower($string);
$string = preg_replace('/[^a-zA-Z0-9]/i','-',$string);
$string = preg_replace("/(-){2,}/",'$1',$string);
return $string;
}
Second thing, you need to make a rewrite, but you should keep aid in form of "/123-title-of-current-page"
Rewrite would go something like this (I am ignoring your entire URL)
RewriteRule ^([0-9]+)-(.*?)$ page.php?aid=$1 [L,QSA]
You can do this using mod_rewrite:
You'll need to edit a file called .htaccess at the top level of your web folder. This is where you can specify certain settings to control the way Apache accesses items in this folder and below.
First things first. Let's turn on mod_rewrite: RewriteEngine On
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
The rule matches any URL which is formed of lower case letters, followed by a /, then more lower case letters and/or hyphens, and appends .php to the end. It keeps track of anything wrapped in brackets () and refers to them later as $1 and $2, i.e. the first and second match. So if someone visits these URLs:
http://example.com/weblog/archive
it will be converted to following:
http://example.com/weblog/archive.php
You will find more details on :
http://wettone.com/code/clean-urls
You have to use a rewrite to direct all requests to an existing php file, otherwise you get all 404 not found errors because you are trying to get a page that simply is not there.
Unless you rewrite your 404 page to handle all requests and you definitely don´t want to go there...

Categories